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

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

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

问题

以下是代码的翻译部分:

我有一个包含两个分类变量和一个数值变量的数据表。

这是生成示例数据的代码:

data <- data.frame(system = rep(c("X","Y","Z"), 10),
                   region = rep(letters[1:5], 6),
                   value = rnorm(60, 500, 300))

现在我想绘制值的系统-区域均值,并将系统均值叠加在系统-区域均值上。

以下是用于绘图数据和第一个绘图的代码:

plot_data <- data %>%
  mutate(system = factor(system), region = factor(region)) %>%
  group_by(system, region) %>%
  summarise(avg = mean(value), .groups = "drop") %>%
  left_join(y = data %>% group_by(system) %>% summarise(avg = mean(value), .groups = "drop"), by = "system", suffix = c("", "_all")) %>%
  mutate(point_type = ifelse(avg_all > avg, "above", "in"))

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

但是现在,如果我想要为geom_point添加颜色美观,就像这样:

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

那么图形不再以与列相同的顺序排列点在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:

data &lt;- data.frame(system = rep(c(&quot;X&quot;,&quot;Y&quot;,&quot;Z&quot;), 10), 
                   region = rep(letters[1:5], 6), 
                   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:

plot_data &lt;- data %&gt;%
  mutate(system = factor(system), region = factor(region)) %&gt;%
  group_by(system, region) %&gt;%
  summarise(avg = mean(value), .groups = &quot;drop&quot;) %&gt;%
  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;%
  mutate(point_type = ifelse(avg_all &gt; avg, &quot;above&quot;, &quot;in&quot;))

ggplot(plot_data, aes(x = region, y = avg, fill = system)) +
  geom_col(position = &quot;dodge&quot;) +
  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:

ggplot(plot_data, aes(x = region, y = avg, fill = system)) +
  geom_col(position = &quot;dodge&quot;) +
  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 进行分组和躲避。

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)
  )

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



<details>
<summary>英文:</summary>

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)
)


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/CAaAP.png

</details>



# 答案2
**得分**: 2

我有点迟到,已经有一个使用 `group` 的答案了。我建议在第一个 `aes` 中使用 `group = system`,这样它将在各个几何图层之间共享更有意义。

另一个选项(虽然不会产生完全相同的图表,我认为这不是一个好的解决方案)是在第一个 `aes` 中定义 `color`,然后在 `geom_col` 中覆盖它。

``` r
ggplot(plot_data, aes(x = region, y = avg, fill = system, color = point_type)) +
  geom_col(position = "dodge", color = "white", size = 0.1) +
  geom_point(aes(y = avg_all), 
             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.

ggplot(plot_data, aes(x = region, y = avg, fill = system, color = point_type)) +
  geom_col(position = &quot;dodge&quot;, color = &quot;white&quot;, size = 0.1) +
  geom_point(aes(y = avg_all), 
             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:

确定