调整在geom_col上绘制的geom_point中变量的顺序。

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

Fix the order of variables in geom_point plotted on top of geom_col

问题

以下是代码的翻译部分:

  1. 我有一个包含两个分类变量和一个数值变量的数据表。
  2. 这是生成示例数据的代码:
  3. data <- data.frame(system = rep(c("X","Y","Z"), 10),
  4. region = rep(letters[1:5], 6),
  5. value = rnorm(60, 500, 300))
  6. 现在我想绘制值的系统-区域均值,并将系统均值叠加在系统-区域均值上。
  7. 以下是用于绘图数据和第一个绘图的代码:
  8. plot_data <- data %>%
  9. mutate(system = factor(system), region = factor(region)) %>%
  10. group_by(system, region) %>%
  11. summarise(avg = mean(value), .groups = "drop") %>%
  12. left_join(y = data %>% group_by(system) %>% summarise(avg = mean(value), .groups = "drop"), by = "system", suffix = c("", "_all")) %>%
  13. mutate(point_type = ifelse(avg_all > avg, "above", "in"))
  14. ggplot(plot_data, aes(x = region, y = avg, fill = system)) +
  15. geom_col(position = "dodge") +
  16. geom_point(aes(y = avg_all), shape = 21, position = position_dodge(width = 0.9))
  17. 但是现在,如果我想要为geom_point添加颜色美观,就像这样:
  18. ggplot(plot_data, aes(x = region, y = avg, fill = system)) +
  19. geom_col(position = "dodge") +
  20. geom_point(aes(y = avg_all, color = point_type), shape = 21, position = position_dodge(width = 0.9))
  21. 那么图形不再以与列相同的顺序排列点在position.dodge内。请注意,在区域'b'中,绿色和蓝色的点/条形物不对齐,在区域'd'中,红色和绿色的点/条形物不对齐,在区域'e'中,红色、绿色和蓝色的点/条形物不对齐。我无法弄清楚原因。不对齐不是系统性的,但我尝试了position = position.dodge2(reverse = T),也没有解决问题。

希望这有助于你理解代码并解决问题。

英文:

I have a data table with two categorical variables and one numeric variable.

Here's code to generate the sample data:

  1. data &lt;- data.frame(system = rep(c(&quot;X&quot;,&quot;Y&quot;,&quot;Z&quot;), 10),
  2. region = rep(letters[1:5], 6),
  3. value = rnorm(60, 500, 300))

Now I want to plot the system-region mean of value AND overlay the system-mean against the system-region mean.

Here is the code to build the data for plotting and the first plot:

  1. plot_data &lt;- data %&gt;%
  2. mutate(system = factor(system), region = factor(region)) %&gt;%
  3. group_by(system, region) %&gt;%
  4. summarise(avg = mean(value), .groups = &quot;drop&quot;) %&gt;%
  5. left_join(y = data %&gt;% group_by(system) %&gt;% summarise(avg = mean(value), .groups = &quot;drop&quot;), by = &quot;system&quot;, suffix = c(&quot;&quot;, &quot;_all&quot;)) %&gt;%
  6. mutate(point_type = ifelse(avg_all &gt; avg, &quot;above&quot;, &quot;in&quot;))
  7. ggplot(plot_data, aes(x = region, y = avg, fill = system)) +
  8. geom_col(position = &quot;dodge&quot;) +
  9. geom_point(aes(y = avg_all), shape = 21, position = position_dodge(width = 0.9))

调整在geom_col上绘制的geom_point中变量的顺序。

But now, if I want to add a color aesthetic to geom_point, like this:

  1. ggplot(plot_data, aes(x = region, y = avg, fill = system)) +
  2. geom_col(position = &quot;dodge&quot;) +
  3. geom_point(aes(y = avg_all, color = point_type), shape = 21, position = position_dodge(width = 0.9))

The graph is no longer arranging the points within position.dodge in the same order as the columns. Note in region 'b' the green and blue points/bars are misaligned, in region 'd' the red and green points/bars are misaligned, and in region 'e' the red, green, and blue points/bars are misaligned. I cannot figure out why. The misalignment is not systematic, but I tried position = position.dodge2(reverse = T) and that did not fix the problem.

调整在geom_col上绘制的geom_point中变量的顺序。

答案1

得分: 2

问题在于通过添加 color aes,您改变了用于 geom_point 的数据分组。要修复这个问题,您需要使用 group aes,告诉 ggplot2 您希望点按 system 进行分组和躲避。

  1. library(ggplot2)
  2. ggplot(plot_data, aes(x = region, y = avg, fill = system)) +
  3. geom_col(position = "dodge") +
  4. geom_point(aes(y = avg_all, color = point_type, group = system),
  5. shape = 21, position = position_dodge(width = 0.9)
  6. )

调整在geom_col上绘制的geom_point中变量的顺序。

  1. <details>
  2. <summary>英文:</summary>
  3. The issue is that by adding the `color` aes you changed the grouping of the data used for `geom_point`. To fix that you have to use the `group` aes, to tell `ggplot2` that you want the points grouped and dodged by `system`.

library(ggplot2)

ggplot(plot_data, aes(x = region, y = avg, fill = system)) +
geom_col(position = "dodge") +
geom_point(aes(y = avg_all, color = point_type, group = system),
shape = 21, position = position_dodge(width = 0.9)
)

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/CAaAP.png
  3. </details>
  4. # 答案2
  5. **得分**: 2
  6. 我有点迟到,已经有一个使用 `group` 的答案了。我建议在第一个 `aes` 中使用 `group = system`,这样它将在各个几何图层之间共享更有意义。
  7. 另一个选项(虽然不会产生完全相同的图表,我认为这不是一个好的解决方案)是在第一个 `aes` 中定义 `color`,然后在 `geom_col` 中覆盖它。
  8. ``` r
  9. ggplot(plot_data, aes(x = region, y = avg, fill = system, color = point_type)) +
  10. geom_col(position = "dodge", color = "white", size = 0.1) +
  11. geom_point(aes(y = avg_all),
  12. shape = 21, position = position_dodge(width = 0.9))

调整在geom_col上绘制的geom_point中变量的顺序。

英文:

I am a little late, and there's an answer using group already. I'd say using group = system in the first aes so it'd be shared between the geoms makes more sense.

Another option (which would not give us the exact same graph and I think would not be a great solution), would be defining color in the first aes and then override it in the geom_col.

  1. ggplot(plot_data, aes(x = region, y = avg, fill = system, color = point_type)) +
  2. geom_col(position = &quot;dodge&quot;, color = &quot;white&quot;, size = 0.1) +
  3. geom_point(aes(y = avg_all),
  4. shape = 21, position = position_dodge(width = 0.9))

调整在geom_col上绘制的geom_point中变量的顺序。

huangapple
  • 本文由 发表于 2023年2月27日 14:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577274.html
匿名

发表评论

匿名网友

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

确定