Custom heatmap theme in R?

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

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

  1. df <- structure(
  2. list(
  3. total_count = c(
  4. 10L,
  5. 0L,
  6. 15L,
  7. 0L,
  8. 20L,
  9. 0L,
  10. 0L,
  11. 50L,
  12. 0L,
  13. 6L,
  14. 1L,
  15. 10L,
  16. 7L,
  17. 0L,
  18. 0L,
  19. 29L,
  20. 0L,
  21. 2L,
  22. 11L,
  23. 3L,
  24. 0L,
  25. 12L,
  26. 0L,
  27. 30L,
  28. 0L,
  29. 0L,
  30. 29L,
  31. 44L,
  32. 10L,
  33. 5L,
  34. 2L,
  35. 145L,
  36. 0L,
  37. 70L
  38. ),
  39. mean_temp = c(
  40. 18.87,
  41. 18.87,
  42. 18.87,
  43. 18.87,
  44. 18.87,
  45. 18.87,
  46. 18.87,
  47. 18.87,
  48. 18.87,
  49. 21.85,
  50. 21.85,
  51. 21.85,
  52. 21.85,
  53. 21.85,
  54. 21.85,
  55. 21.85,
  56. 21.85,
  57. 21.85,
  58. 17.11,
  59. 17.11,
  60. 17.11,
  61. 17.11,
  62. 17.11,
  63. 17.11,
  64. 17.11,
  65. 17.11,
  66. 18.82,
  67. 18.82,
  68. 18.82,
  69. 18.82,
  70. 18.82,
  71. 18.82,
  72. 18.82,
  73. 18.82
  74. ),
  75. lwd_duration = c(
  76. 64.32,
  77. 64.32,
  78. 64.32,
  79. 64.32,
  80. 64.32,
  81. 64.32,
  82. 64.32,
  83. 64.32,
  84. 64.32,
  85. 104.2,
  86. 104.2,
  87. 104.2,
  88. 104.2,
  89. 104.2,
  90. 104.2,
  91. 104.2,
  92. 104.2,
  93. 104.2,
  94. 53.53,
  95. 53.53,
  96. 53.53,
  97. 53.53,
  98. 53.53,
  99. 53.53,
  100. 53.53,
  101. 53.53,
  102. 60.43,
  103. 60.43,
  104. 60.43,
  105. 60.43,
  106. 60.43,
  107. 60.43,
  108. 60.43,
  109. 60.43
  110. )
  111. ),
  112. row.names = c(NA,-34L),
  113. class = c("tbl_df", "tbl", "data.frame"),
  114. na.action = structure(
  115. c(
  116. `4` = 4L,
  117. `5` = 5L,
  118. `6` = 6L,
  119. `7` = 7L,
  120. `8` = 8L,
  121. `9` = 9L,
  122. `78` = 78L,
  123. `87` = 87L,
  124. `96` = 96L,
  125. `105` = 105L,
  126. `114` = 114L,
  127. `123` = 123L,
  128. `132` = 132L,
  129. `141` = 141L,
  130. `150` = 150L,
  131. `159` = 159L,
  132. `168` = 168L,
  133. `177` = 177L,
  134. `186` = 186L,
  135. `849` = 849L,
  136. `850` = 850L,
  137. `851` = 851L,
  138. `852` = 852L,
  139. `891` = 891L,
  140. `892` = 892L,
  141. `893` = 893L,
  142. `894` = 894L,
  143. `921` = 921L,
  144. `922` = 922L,
  145. `923` = 923L,
  146. `924` = 924L,
  147. `937` = 937L,
  148. `938` = 938L,
  149. `939` = 939L,
  150. `940` = 940L,
  151. `969` = 969L,
  152. `970` = 970L,
  153. `971` = 971L,
  154. `972` = 972L,
  155. `985` = 985L,
  156. `986` = 986L,
  157. `987` = 987L,
  158. `988` = 988L,
  159. `1017` = 1017L,
  160. `1018` = 1018L,
  161. `1019` = 1019L,
  162. `1020` = 1020L,
  163. `1033` = 1033L,
  164. `1034` = 1034L,
  165. `1035` = 1035L,
  166. `1036` = 1036L
  167. ),
  168. class = "omit"
  169. )
  170. )

Code used to produce the figure:

  1. dat2 <-
  2. df %>%
  3. as_tibble() %>%
  4. mutate(
  5. mean_temp = cut_interval(mean_temp, n = 10),
  6. total_count = cut_interval(total_count, n = 10),
  7. ) %>%
  8. group_by(mean_temp, total_count) %>%
  9. summarize(lwd_duration = mean(lwd_duration))
  10. #> `summarise()` has grouped output by 'mean_temp'. You can override using the
  11. #> `.groups` argument.
  12. ggplot(dat2, aes(mean_temp, total_count)) +
  13. geom_tile(aes(fill = lwd_duration)) +
  14. geom_text(aes(label = round(lwd_duration, 1))) +
  15. 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

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

  1. library(ggplot2)
  2. library(dplyr)
  3. df %>%
  4. mutate(
  5. mean_temp = cut_interval(mean_temp, n = 10),
  6. total_count = cut_interval(total_count, n = 10),
  7. ) %>%
  8. group_by(mean_temp, total_count) %>%
  9. summarize(lwd_duration = mean(lwd_duration)) %>%
  10. ggplot(aes(mean_temp, total_count, fill = lwd_duration)) +
  11. geom_tile(color = 'white', linewidth = 1.5) +
  12. geom_text(aes(label = round(lwd_duration, 1))) +
  13. scale_fill_gradient(low = "white", high = "red") +
  14. theme(panel.grid.major = element_blank(),
  15. panel.background = element_rect(fill = 'white'))

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

英文:
  1. library(ggplot2)
  2. library(dplyr)
  3. df %>%
  4. mutate(
  5. mean_temp = cut_interval(mean_temp, n = 10),
  6. total_count = cut_interval(total_count, n = 10),
  7. ) %>%
  8. group_by(mean_temp, total_count) %>%
  9. summarize(lwd_duration = mean(lwd_duration)) %>%
  10. ggplot(aes(mean_temp, total_count, fill = lwd_duration)) +
  11. geom_tile(color = 'white', linewidth = 1.5) +
  12. geom_text(aes(label = round(lwd_duration, 1))) +
  13. scale_fill_gradient(low = "white", high = "red") +
  14. theme(panel.grid.major = element_blank(),
  15. panel.background = element_rect(fill = 'white'))
  16. #> `summarise()` has grouped output by 'mean_temp'. You can override using the
  17. #> `.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:

确定