Custom heatmap theme in R?

huangapple go评论61阅读模式
英文:

Custom heatmap theme in R?

问题

I understand that you're looking for a translation of the content you provided without the code. Here's the translated text:

以下是要翻译的内容:

下面显示的图表中使用了什么主题,或者可以使用什么类似的主题来创建一个吸引人的图表,就像提供的照片中的那个一样?我在互联网上看到了这张照片,但没有提供代码。我已经找到了代码,但目前的主题难以解释。

如上所示,每个“周”和“年龄”组合都有一个单独的方框。非常容易看出某个年龄组在特定周的COVID数字是多少。有人可以告诉我以下主题是什么,或者建议一个类似的主题来改进图表的外观,以适应科学观众吗?

这是我的数据的可重现示例:

在我的图表中,对于“mean_temp” [17.17, 17.6] 和“total_count” [0, 14.5] 的组合,应该有一个单独的方框,其中“total_count”为53.5。具有值的单元格已合并,因此难以解释。我不希望个别方框重叠。每个单元格中有多个观察结果。假设对于“mean_temp”的一个特定范围,与“total_count”的一个范围相结合,我有值5、15、100,所以我必须使用平均数。感谢任何帮助。

英文:

What is the theme used in the graph shown below, or what similar theme can be used to create an appealing graph like the one in the provided photo? I saw the photo on the internet but no code was given. I have already sourced the code, but it's hard to interpret with the current theme.

Custom heatmap theme in R?

As shown above, There is a separate box for each week and age combination. It is very easy to tell what were covid number in a particular week for a certain age group. Can someone tell what's the following theme, or suggest a similar theme to improve the graph's appearance for scientific audience?

Here is the reproducible example of my data

df <- structure(
  list(
    total_count = c(
      10L,
      0L,
      15L,
      0L,
      20L,
      0L,
      0L,
      50L,
      0L,
      6L,
      1L,
      10L,
      7L,
      0L,
      0L,
      29L,
      0L,
      2L,
      11L,
      3L,
      0L,
      12L,
      0L,
      30L,
      0L,
      0L,
      29L,
      44L,
      10L,
      5L,
      2L,
      145L,
      0L,
      70L
    ),
    mean_temp = c(
      18.87,
      18.87,
      18.87,
      18.87,
      18.87,
      18.87,
      18.87,
      18.87,
      18.87,
      21.85,
      21.85,
      21.85,
      21.85,
      21.85,
      21.85,
      21.85,
      21.85,
      21.85,
      17.11,
      17.11,
      17.11,
      17.11,
      17.11,
      17.11,
      17.11,
      17.11,
      18.82,
      18.82,
      18.82,
      18.82,
      18.82,
      18.82,
      18.82,
      18.82
    ),
    lwd_duration = c(
      64.32,
      64.32,
      64.32,
      64.32,
      64.32,
      64.32,
      64.32,
      64.32,
      64.32,
      104.2,
      104.2,
      104.2,
      104.2,
      104.2,
      104.2,
      104.2,
      104.2,
      104.2,
      53.53,
      53.53,
      53.53,
      53.53,
      53.53,
      53.53,
      53.53,
      53.53,
      60.43,
      60.43,
      60.43,
      60.43,
      60.43,
      60.43,
      60.43,
      60.43
    )
  ),
  row.names = c(NA,-34L),
  class = c("tbl_df", "tbl", "data.frame"),
  na.action = structure(
    c(
      `4` = 4L,
      `5` = 5L,
      `6` = 6L,
      `7` = 7L,
      `8` = 8L,
      `9` = 9L,
      `78` = 78L,
      `87` = 87L,
      `96` = 96L,
      `105` = 105L,
      `114` = 114L,
      `123` = 123L,
      `132` = 132L,
      `141` = 141L,
      `150` = 150L,
      `159` = 159L,
      `168` = 168L,
      `177` = 177L,
      `186` = 186L,
      `849` = 849L,
      `850` = 850L,
      `851` = 851L,
      `852` = 852L,
      `891` = 891L,
      `892` = 892L,
      `893` = 893L,
      `894` = 894L,
      `921` = 921L,
      `922` = 922L,
      `923` = 923L,
      `924` = 924L,
      `937` = 937L,
      `938` = 938L,
      `939` = 939L,
      `940` = 940L,
      `969` = 969L,
      `970` = 970L,
      `971` = 971L,
      `972` = 972L,
      `985` = 985L,
      `986` = 986L,
      `987` = 987L,
      `988` = 988L,
      `1017` = 1017L,
      `1018` = 1018L,
      `1019` = 1019L,
      `1020` = 1020L,
      `1033` = 1033L,
      `1034` = 1034L,
      `1035` = 1035L,
      `1036` = 1036L
    ),
    class = "omit"
  )
)

Code used to produce the figure:

dat2 <-
  df %>%
  as_tibble() %>%
  mutate(
    mean_temp = cut_interval(mean_temp, n = 10),
    total_count = cut_interval(total_count, n = 10),
  ) %>%
  group_by(mean_temp, total_count) %>%
  summarize(lwd_duration = mean(lwd_duration))
#> `summarise()` has grouped output by 'mean_temp'. You can override using the
#> `.groups` argument.

ggplot(dat2, aes(mean_temp, total_count)) +
  geom_tile(aes(fill = lwd_duration)) +
  geom_text(aes(label = round(lwd_duration, 1))) +
  scale_fill_gradient(low = "white", high = "red")

Custom heatmap theme in R?

In my graph, I should have a separate box for the mean_temp [17.17, 17.6] and total_count [0, 14.5] combination with total_count 53.5 inside the box. Cells with values have merged, so it's hard to interpret. I don't want individual boxes overlapping. I have multiple observations in each cell. Suppose for one particular range of mean_temp combined with one range of total_count, I have values 5, 15, 100, so I had to use mean numbers. Thanks for any assistance.

答案1

得分: 0

以下是您要翻译的代码部分:

library(ggplot2)
library(dplyr)

df %>%
  mutate(
    mean_temp = cut_interval(mean_temp, n = 10),
    total_count = cut_interval(total_count, n = 10),
  ) %>%
  group_by(mean_temp, total_count) %>%
  summarize(lwd_duration = mean(lwd_duration)) %>%
  ggplot(aes(mean_temp, total_count, fill = lwd_duration)) +
  geom_tile(color = 'white', linewidth = 1.5) +
  geom_text(aes(label = round(lwd_duration, 1))) +
  scale_fill_gradient(low = "white", high = "red") +
  theme(panel.grid.major = element_blank(),
        panel.background = element_rect(fill = 'white'))

请注意,这是R编程语言中的一段代码,用于创建可视化图表。

英文:
library(ggplot2)
library(dplyr)


df %>%
  mutate(
    mean_temp = cut_interval(mean_temp, n = 10),
    total_count = cut_interval(total_count, n = 10),
  ) %>%
  group_by(mean_temp, total_count) %>%
  summarize(lwd_duration = mean(lwd_duration)) %>%
  ggplot(aes(mean_temp, total_count, fill = lwd_duration)) +
  geom_tile(color = 'white', linewidth = 1.5) +
  geom_text(aes(label = round(lwd_duration, 1))) +
  scale_fill_gradient(low = "white", high = "red") +
  theme(panel.grid.major = element_blank(),
        panel.background = element_rect(fill = 'white'))
#> `summarise()` has grouped output by 'mean_temp'. You can override using the
#> `.groups` argument.

Custom heatmap theme in R?<!-- -->

<sup>Created on 2023-04-19 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年4月20日 00:45:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定