ggplot2:在aes()内将一个分组变量的一个级别拆分为多个geom项?

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

ggplot2: Splitting 1 level of grouping variable into multiple geom items within aes()?

问题

  1. 我有一个适合我的情节,但需要一些帮助将所有的`geom_line``ribbon`放在同一个图例中,就像下面的图例图片一样(在Excel中完成)。据我了解,ggplot使用`aes()`函数和颜色/填充命令将项目放入图例中,但每行数据都需要一个唯一的分组变量。我认为我的结果情节会要求ggplot尝试将相同的组别级别拆分为两种不同的美学效果(从2个分组级别生成4条线?)。但我不确定如何在不重组数据的情况下创建这个情节,并且不会失去我到目前为止所做的事情。本质上,我试图将我的“q2”列按“group”变量(High/Low)分组,就像我的geom_ribbon一样,但是它们有自己的线条和颜色(称为“中位数(高年)”和“中位数(低年)”)。此外,我希望将第二个数据框(“blackline”)也添加到图例中。
  2. 我的目标:
  3. [![enter image description here][1]][1]
  4. 尝试:
  5. [![enter image description here][2]][2]
  6. 情节:
  7. ```R
  8. library(ggplot2)
  9. ggplot(ribbons2, aes(x=Month, fill=group)) +
  10. geom_ribbon(aes(ymin=q1,ymax=q3), alpha=0.4) +
  11. geom_line(aes(x=Month, y=q2, fill =group, color = c("#DC143C", "#556B2F")), linewidth = 0.4, linetype = "dashed") +
  12. geom_line(data=blackline, mapping=aes(x=Month, y=q2, fill = group), inherit.aes = FALSE, linewidth = 0.8) +
  13. facet_wrap(factor(Zone)~ ., labeller=as_labeller(c("West" = "West",
  14. "Whipray" = "Whipray",
  15. "Rankin" = "Rankin",
  16. "Crocodile" = "Crocodile Dragover*")),
  17. scales='free') +
  18. scale_x_continuous(breaks=c(5,6,7,8,9,10,11)) +
  19. scale_y_continuous(expand = c(0, 0),
  20. limits=c(0, 1),
  21. breaks=c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0)) +
  22. scale_fill_manual(name="", labels=c('25th - 75th percentile (High years)',
  23. '25th - 75th percentile (Low years)'),
  24. values=c("#d8e4bc", "#e8bcbc")) +
  25. theme_classic()+
  26. theme(strip.text = element_text(face = "bold"),
  27. legend.justification = "top",
  28. strip.background = element_blank(),
  29. axis.title.y = element_text(face="bold", margin = margin(t = 0, r = 10, b = 0, l = 0)),
  30. axis.title.x = element_text(face="bold", margin = margin(t = 10, r = 0, b = 0, l = 0)),
  31. axis.text.x = element_text(size = 8),
  32. axis.text.y = element_text(size = 7)) +
  33. ylab("Frequency of Occurence")

错误:

  1. Error in `geom_line()`:
  2. ! Problem while computing aesthetics.
  3. Error occurred in the 2nd layer.
  4. Caused by error in `check_aesthetics()`:
  5. ! Aesthetics must be either length 1 or the same as the data (55)
  6. Fix the following mappings: `colour`
  7. Run `rlang::last_trace()` to see where the error occurred.
  8. Warning messages:
  9. 1: In geom_line(aes(x = Month, y = q2, fill = group, color = c("#DC143C", :
  10. Ignoring unknown aesthetics: fill
  11. 2: In geom_line(data = blackline, mapping = aes(x = Month, y = q2, :
  12. Ignoring unknown aesthetics: fill

数据(2个数据框):

  1. dput(blackline)
  2. structure(list(Zone = c("Crocodile", "Crocodile", "Crocodile",
  3. "Crocodile", "Crocodile", "Crocodile", "Crocodile", "Rankin",
  4. "Rankin", "Rankin", "Rankin", "Rankin", "Rankin", "Rankin", "West",
  5. "West", "West", "West", "West", "West", "West", "Whipray", "Whipray",
  6. "Whipray", "Whipray", "Whipray", "Whipray", "Whipray"), Month = c(5,
  7. 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10,
  8. 11, 5, 6, 7, 8, 9, 10, 11), q2 = c(`50%` = 0, `50%` = 0, `50%` = 0,
  9. `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0.0909,
  10. `50%` = 0.1818, `50%` = 0.1364, `50%` = 0.0909, `50%` = 0.2727,
  11. `50%` = 0.1818, `50%` = 0.05, `50%` = 0.1, `50%` = 0.2, `50%` = 0.25,
  12. `50%` = 0.05, `50%` = 0.1, `50%` = 0.05, `50%` = 0, `50%` = 0,
  13. `50%` = 0, `50%` = 0.1053, `50%` = 0, `50%` = 0, `50%` = 0),
  14. group = c("2021", "2021", "2021", "2021", "2021", "2021",
  15. "2021", "2021", "2021", "2021", "2021", "
  16. <details>
  17. <summary>英文:</summary>
  18. I have a plot that works for me, but need some help putting all the `geom_line`(s) and `ribbon`(s) in the same legend, like the image of the legend below (done in Excel). As I understand it, ggplot puts items in a legend using the aes() function and the color/fill commands, but each row of data needs a unique grouping variable. I think my resultant plot would be asking ggplot to try and split the same group level into two different aesthetics (making 4 lines from 2 grouping levels?). I&#39;m not sure how to create this though without restructuring the data and loosing what I made so far. Essentially, I&#39;m trying to group my &quot;q2&quot; column by the &quot;group&quot; variable (High/Low) like my geom_ribbon, but with their own lines and colors (called &quot;Median (High Years)&quot; and &quot;Median (Low Years)&quot;). Additionally, I hope to add the secondary dataframe (&quot;blackline&quot;) it to the legend as well.
  19. **My goal:**
  20. [![enter image description here][1]][1]
  21. **Attempt:**
  22. [![enter image description here][2]][2]
  23. Plot:
  24. library(ggplot2)
  25. ggplot(ribbons2, aes(x=Month, fill=group)) +
  26. geom_ribbon(aes(ymin=q1,ymax=q3), alpha=0.4) +
  27. geom_line(aes(x=Month, y=q2, fill =group, color = c(&quot;#DC143C&quot;, &quot;#556B2F&quot;)), linewidth = 0.4, linetype = &quot;dashed&quot;) +
  28. geom_line(data=blackline, mapping=aes(x=Month, y=q2, fill = group), inherit.aes = FALSE, linewidth = 0.8) +
  29. facet_wrap(factor(Zone)~ ., labeller=as_labeller(c(&quot;West&quot; = &quot;West&quot;,
  30. &quot;Whipray&quot; = &quot;Whipray&quot;,
  31. &quot;Rankin&quot; = &quot;Rankin&quot;,
  32. &quot;Crocodile&quot; = &quot;Crocodile Dragover*&quot;)),
  33. scales=&#39;free&#39;) +
  34. scale_x_continuous(breaks=c(5,6,7,8,9,10,11)) +
  35. scale_y_continuous(expand = c(0, 0),
  36. limits=c(0, 1),
  37. breaks=c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0)) +
  38. scale_fill_manual(name=&quot;&quot;, labels=c(&#39;25th - 75th percentile (High years)&#39;,
  39. &#39;25th - 75th percentile (Low years)&#39;),
  40. values=c(&quot;#d8e4bc&quot;, &quot;#e8bcbc&quot;)) +
  41. theme_classic()+
  42. theme(strip.text = element_text(face = &quot;bold&quot;),
  43. legend.justification = &quot;top&quot;,
  44. strip.background = element_blank(),
  45. axis.title.y = element_text(face=&quot;bold&quot;, margin = margin(t = 0, r = 10, b = 0, l = 0)),
  46. axis.title.x = element_text(face=&quot;bold&quot;, margin = margin(t = 10, r = 0, b = 0, l = 0)),
  47. axis.text.x = element_text(size = 8),
  48. axis.text.y = element_text(size = 7)) +
  49. ylab(&quot;Frequency of Occurence&quot;)
  50. Error:
  51. Error in `geom_line()`:
  52. ! Problem while computing aesthetics.
  53. ℹ Error occurred in the 2nd layer.
  54. Caused by error in `check_aesthetics()`:
  55. ! Aesthetics must be either length 1 or the same as the data (55)
  56. ✖ Fix the following mappings: `colour`
  57. Run `rlang::last_trace()` to see where the error occurred.
  58. Warning messages:
  59. 1: In geom_line(aes(x = Month, y = q2, fill = group, color = c(&quot;#DC143C&quot;, :
  60. Ignoring unknown aesthetics: fill
  61. 2: In geom_line(data = blackline, mapping = aes(x = Month, y = q2, :
  62. Ignoring unknown aesthetics: fill
  63. Data (2 data.frames):
  64. &gt; dput(blackline)
  65. structure(list(Zone = c(&quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Crocodile&quot;,
  66. &quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Rankin&quot;,
  67. &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;West&quot;,
  68. &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;,
  69. &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;), Month = c(5,
  70. 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10,
  71. 11, 5, 6, 7, 8, 9, 10, 11), q2 = c(`50%` = 0, `50%` = 0, `50%` = 0,
  72. `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0.0909,
  73. `50%` = 0.1818, `50%` = 0.1364, `50%` = 0.0909, `50%` = 0.2727,
  74. `50%` = 0.1818, `50%` = 0.05, `50%` = 0.1, `50%` = 0.2, `50%` = 0.25,
  75. `50%` = 0.05, `50%` = 0.1, `50%` = 0.05, `50%` = 0, `50%` = 0,
  76. `50%` = 0, `50%` = 0.1053, `50%` = 0, `50%` = 0, `50%` = 0),
  77. group = c(&quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;,
  78. &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;,
  79. &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;,
  80. &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;)), row.names = c(NA,
  81. -28L), class = c(&quot;grouped_df&quot;, &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), groups = structure(list(
  82. Zone = c(&quot;Crocodile&quot;, &quot;Rankin&quot;, &quot;West&quot;, &quot;Whipray&quot;), .rows = structure(list(
  83. 1:7, 8:14, 15:21, 22:28), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;,
  84. &quot;vctrs_vctr&quot;, &quot;list&quot;))), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;
  85. ), row.names = c(NA, -4L), .drop = TRUE))
  86. &gt; dput(ribbons)
  87. structure(list(Zone = c(&quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Crocodile&quot;,
  88. &quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Rankin&quot;,
  89. &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;West&quot;,
  90. &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;,
  91. &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Crocodile&quot;,
  92. &quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Crocodile&quot;, &quot;Crocodile&quot;,
  93. &quot;Crocodile&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;Rankin&quot;,
  94. &quot;Rankin&quot;, &quot;Rankin&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;,
  95. &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;, &quot;Whipray&quot;,
  96. &quot;Whipray&quot;), Month = c(5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10,
  97. 11, 5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8,
  98. 9, 10, 11, 5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10, 5, 6, 7,
  99. 8, 9, 10, 11), q1 = c(`25%` = 0, `25%` = 0, `25%` = 0.05, `25%` = 0,
  100. `25%` = 0.1, `25%` = 0.0625, `25%` = 0.0125, `25%` = 0.0909,
  101. `25%` = 0.086, `25%` = 0.0649, `25%` = 0.2273, `25%` = 0.2208,
  102. `25%` = 0.4302, `25%` = 0.2727, `25%` = 0.05, `25%` = 0.2, `25%` = 0.25,
  103. `25%` = 0.25, `25%` = 0.3636, `25%` = 0.25, `25%` = 0.15, `25%` = 0.0132,
  104. `25%` = 0, `25%` = 0.0789, `25%` = 0.1096, `25%` = 0.2763, `25%` = 0.2868,
  105. `25%` = 0.0395, `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0,
  106. `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0,
  107. `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0.05,
  108. `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0, `25%` = 0,
  109. `25%` = 0, `25%` = 0, `25%` = 0.0132, `25%` = 0), q2 = c(`50%` = 0,
  110. `50%` = 0, `50%` = 0.05, `50%` = 0, `50%` = 0.1, `50%` = 0.1,
  111. `50%` = 0.075, `50%` = 0.1364, `50%` = 0.1136, `50%` = 0.1357,
  112. `50%` = 0.2273, `50%` = 0.2273, `50%` = 0.4773, `50%` = 0.2792,
  113. `50%` = 0.075, `50%` = 0.25, `50%` = 0.25, `50%` = 0.25, `50%` = 0.3684,
  114. `50%` = 0.2857, `50%` = 0.1923, `50%` = 0.0263, `50%` = 0.0526,
  115. `50%` = 0.3026, `50%` = 0.2056, `50%` = 0.4079, `50%` = 0.3684,
  116. `50%` = 0.1316, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0,
  117. `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0,
  118. `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0.05,
  119. `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0, `50%` = 0,
  120. `50%` = 0, `50%` = 0, `50%` = 0.0526, `50%` = 0), q3 = c(`75%` = 0,
  121. `75%` = 0, `75%` = 0.1, `75%` = 0.0125, `75%` = 0.15, `75%` = 0.175,
  122. `75%` = 0.1, `75%` = 0.1818, `75%` = 0.1591, `75%` = 0.2295,
  123. `75%` = 0.2922, `75%` = 0.2273, `75%` = 0.5114, `75%` = 0.3929,
  124. `75%` = 0.15, `75%` = 0.35, `75%` = 0.3, `75%` = 0.25, `75%` = 0.45,
  125. `75%` = 0.35, `75%` = 0.2, `75%` = 0.0395, `75%` = 0.1289, `75%` = 0.55,
  126. `75%` = 0.375, `75%` = 0.575, `75%` = 0.4658, `75%` = 0.2329,
  127. `75%` = 0, `75%` = 0, `75%` = 0, `75%` = 0, `75%` = 0, `75%` = 0,
  128. `75%` = 0, `75%` = 0, `75%` = 0, `75%` = 0, `75%` = 0.0109, `75%` = 0,
  129. `75%` = 0, `75%` = 0, `75%` = 0, `75%` = 0, `75%` = 0.05, `75%` = 0,
  130. `75%` = 0, `75%` = 0, `75%` = 0, `75%` = 0, `75%` = 0, `75%` = 0,
  131. `75%` = 0, `75%` = 0.0882, `75%` = 0), group = c(&quot;high&quot;, &quot;high&quot;,
  132. &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;,
  133. &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;,
  134. &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;, &quot;high&quot;,
  135. &quot;high&quot;, &quot;high&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;,
  136. &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;,
  137. &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;, &quot;low&quot;,
  138. &quot;low&quot;, &quot;low&quot;)), row.names = c(NA, -55L), groups = structure(list(
  139. Zone = c(&quot;Crocodile&quot;, &quot;Rankin&quot;, &quot;West&quot;, &quot;Whipray&quot;), .rows = structure(list(
  140. c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 29L, 30L, 31L, 32L, 33L,
  141. 34L, 35L), c(8L, 9L, 10L, 11L, 12L, 13L, 14L, 36L, 37L,
  142. 38L, 39L, 40L, 41L, 42L), c(15L, 16L, 17L, 18L, 19L,
  143. 20L, 21L, 43L, 44L, 45L, 46L, 47L, 48L), c(22L, 23L,
  144. 24L, 25L, 26L, 27L, 28L, 49L, 50L, 51L, 52L, 53L, 54L,
  145. 55L)), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;,
  146. &quot;vctrs_vctr&quot;, &quot;list&quot;))), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;
  147. ), row.names = c(NA, -4L), .drop = TRUE), class = c(&quot;grouped_df&quot;,
  148. &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  149. [1]: https://i.stack.imgur.com/VuBhs.png
  150. [2]: https://i.stack.imgur.com/rSWh8.png
  151. </details>
  152. # 答案1
  153. **得分**: 1
  154. 将混合不同美学和几何元素时,正确显示图例始终有点棘手。首先,由于您的图例应显示5个不同的类别,仅将`group`映射到美学上是不足够的。相反,我们必须为彩色线条创建两个更多的类别,以及一个黑色线条的类别。对于彩色线条,我使用`paste0(group, " _line ")`,而对于黑色线条,我只是使用` "2021" `。其次,为了确保不同美学(即`color`和`fill`)的图例合并,我确保每个图例中都显示了所有五个类别,为此我使用了`limits`参数。当然,我们需要相同的`labels`。此外,我们还需要为每个类别分配一个值。最后,为了在图例中反映不同的线型和线宽,我还在`linetype`和`linewidth`美学上进行了映射。
  155. ```R
  156. library(ggplot2)
  157. limits <- c("high", "high_line", "2021", "low", "low_line")
  158. labels <- c(
  159. high = "25th - 75th percentile (High years)",
  160. low = "25th - 75th percentile (Low years)",
  161. high_line = "Median (High years)",
  162. low_line = "Median (Low years)",
  163. "2021" = "2021"
  164. )
  165. ggplot(ribbons, aes(x = Month, fill = group)) +
  166. geom_ribbon(aes(ymin = q1, ymax = q3), alpha = 0.4) +
  167. geom_line(
  168. aes(
  169. x = Month, y = q2,
  170. color = paste0(group, "_line"),
  171. linetype = paste0(group, "_line"),
  172. linewidth = paste0(group, "_line")
  173. )
  174. ) +
  175. geom_line(
  176. data = blackline, mapping = aes(
  177. x = Month, y = q2, color = "2021",
  178. linetype = "2021",
  179. linewidth = "2021"
  180. ),
  181. inherit.aes = FALSE
  182. ) +
  183. facet_wrap(factor(Zone) ~ .,
  184. labeller = as_labeller(c(
  185. "West" = "West",
  186. "Whipray" = "Whipray",
  187. "Rankin" = "Rankin",
  188. "Crocodile" = "Crocodile Dragover*"
  189. )),
  190. scales = "free"
  191. ) +
  192. scale_x_continuous(breaks = c(5, 6, 7, 8, 9, 10, 11)) +
  193. scale_y_continuous(
  194. expand = c(0, 0),
  195. limits = c(0, 1),
  196. breaks = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)
  197. ) +
  198. scale_fill_manual(
  199. limits = limits,
  200. labels = labels,
  201. values = c(
  202. high = "#d8e4bc", low = "#e8bcbc",
  203. high_line = "transparent", low_line = "transparent", "2021" = "transparent"
  204. )
  205. ) +
  206. scale_color_manual(
  207. limits = limits,
  208. labels = labels,
  209. values = c(
  210. low_line = "#DC143C", high_line = "#556B2F",
  211. low = "transparent", high = "transparent", "2021" = "black"
  212. )
  213. ) +
  214. scale_linetype_manual(
  215. limits = limits,
  216. labels = labels,
  217. values = c(
  218. low_line = "dashed", high_line = "dashed",
  219. low = "blank", high = "blank", "2021" = "solid"
  220. )
  221. ) +
  222. scale_linewidth_manual(
  223. limits = limits,
  224. labels = labels,
  225. values = c(
  226. low_line = .4, high_line = .4,
  227. low = .1, high = .1, "2021" = .8
  228. )
  229. ) +
  230. theme_classic() +
  231. theme(
  232. strip.text = element_text(face = "bold"),
  233. legend.justification = "top",
  234. strip.background = element_blank(),
  235. axis.title.y = element_text(face = "bold", margin = margin(t = 0, r = 10, b = 0, l = 0)),
  236. axis.title.x = element_text(face = "bold", margin = margin(t = 10, r = 0, b = 0, l = 0)),
  237. axis.text.x = element_text(size = 8),
  238. axis.text.y = element_text(size = 7)
  239. ) +
  240. labs(y = "Frequency of Occurence", color = NULL, fill = NULL, linetype = NULL, linewidth = NULL)

ggplot2:在aes()内将一个分组变量的一个级别拆分为多个geom项?

  1. <details>
  2. <summary>英文:</summary>
  3. Getting the legend right when mixing different aesthetics and geoms is always a bit tricky. First, as your legend should display 5 different categories, simply mapping `group` on aesthetics isn&#39;t sufficient. Instead we have to crate two more categories for the colored lines and one for the black line. For the colored ones I use `paste0(group, &quot;_line&quot;)` whereas for the black one I simply use `&quot;2021&quot;`. Second, to make sure that the legends for the different aesthetics, i.e. `color` and `fill` get merged I make sure that all five categories are displayed in each legend for which I use the `limits` argument. And of course do we need the same `labels` for each. Moreover, we have to assign a value to each category. Finally, for the perfect look at to reflect the different linetypes and line widths in the legend too I also map on the `linetype` and the `linewidth` aesthetic.

library(ggplot2)

limits <- c("high", "high_line", "2021", "low", "low_line")
labels <- c(
high = "25th - 75th percentile (High years)",
low = "25th - 75th percentile (Low years)",
high_line = "Median (High years)",
low_line = "Median (Low years)",
"2021" = "2021"
)

ggplot(ribbons, aes(x = Month, fill = group)) +
geom_ribbon(aes(ymin = q1, ymax = q3), alpha = 0.4) +
geom_line(
aes(
x = Month, y = q2,
color = paste0(group, "_line"),
linetype = paste0(group, "_line"),
linewidth = paste0(group, "_line")
)
) +
geom_line(
data = blackline, mapping = aes(
x = Month, y = q2, color = "2021",
linetype = "2021",
linewidth = "2021"
),
inherit.aes = FALSE
) +
facet_wrap(factor(Zone) ~ .,
labeller = as_labeller(c(
"West" = "West",
"Whipray" = "Whipray",
"Rankin" = "Rankin",
"Crocodile" = "Crocodile Dragover*"
)),
scales = "free"
) +
scale_x_continuous(breaks = c(5, 6, 7, 8, 9, 10, 11)) +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, 1),
breaks = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)
) +
scale_fill_manual(
limits = limits,
labels = labels,
values = c(
high = "#d8e4bc", low = "#e8bcbc",
high_line = "transparent", low_line = "transparent", "2021" = "transparent"
)
) +
scale_color_manual(
limits = limits,
labels = labels,
values = c(
low_line = "#DC143C", high_line = "#556B2F",
low = "transparent", high = "transparent", "2021" = "black"
)
) +
scale_linetype_manual(
limits = limits,
labels = labels,
values = c(
low_line = "dashed", high_line = "dashed",
low = "blank", high = "blank", "2021" = "solid"
)
) +
scale_linewidth_manual(
limits = limits,
labels = labels,
values = c(
low_line = .4, high_line = .4,
low = .1, high = .1, "2021" = .8
)
) +
theme_classic() +
theme(
strip.text = element_text(face = "bold"),
legend.justification = "top",
strip.background = element_blank(),
axis.title.y = element_text(face = "bold", margin = margin(t = 0, r = 10, b = 0, l = 0)),
axis.title.x = element_text(face = "bold", margin = margin(t = 10, r = 0, b = 0, l = 0)),
axis.text.x = element_text(size = 8),
axis.text.y = element_text(size = 7)
) +
labs(y = "Frequency of Occurence", color = NULL, fill = NULL, linetype = NULL, linewidth = NULL)

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/roHDM.png
  3. </details>

huangapple
  • 本文由 发表于 2023年6月19日 23:37:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76508141.html
匿名

发表评论

匿名网友

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

确定