如何为使用ggplot2显示均值的柱状图添加单独的误差线。

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

How do i add seperate error bars for a column plot displaying means in ggplot2

问题

让我们假设我的数据框是这样创建的:

  1. df=data.frame(type=as.factor(c("tree","tree","tree","tree", "bush","bush","bush", "bush","flower", "flower", "flower", "flower")),
  2. age=as.factor(c(5,1,5,1,5,1,5,1,5,1,5,1)),
  3. size=c(11,3,16,5,2,0.5,3,0.4,1,0.2,0.9,0.15))

现在它看起来是这样的:

  1. > df
  2. type age size
  3. 1 tree 5 11.00
  4. 2 tree 1 3.00
  5. 3 tree 5 16.00
  6. 4 tree 1 5.00
  7. 5 bush 5 2.00
  8. 6 bush 1 0.50
  9. 7 bush 5 3.00
  10. 8 bush 1 0.40
  11. 9 flower 5 1.00
  12. 10 flower 1 0.20
  13. 11 flower 5 0.90
  14. 12 flower 1 0.15

我希望我的 x 轴比较年龄因子 5 和 1,同时让类型因子相邻显示:

  1. plot = ggplot(data = df, aes(x = age, y = size, fill=type))
  2. plot = plot + geom_col(position = "dodge")
  3. plot

到目前为止都很好

现在我在使用 geom_errorbar() 为我的柱状图应用合理的误差栏时遇到了困难。它们应该显示每列的标准偏差。

英文:

Let's say my data frame was created like this:

  1. df=data.frame(type=as.factor(c("tree","tree","tree","tree", "bush","bush","bush", "bush","flower", "flower", "flower", "flower")),
  2. age=as.factor(c(5,1,5,1,5,1,5,1,5,1,5,1)),
  3. size=c(11,3,16,5,2,0.5,3,0.4,1,0.2,0.9,0.15))

and now it looks like that:

  1. > df
  2. type age size
  3. 1 tree 5 11.00
  4. 2 tree 1 3.00
  5. 3 tree 5 16.00
  6. 4 tree 1 5.00
  7. 5 bush 5 2.00
  8. 6 bush 1 0.50
  9. 7 bush 5 3.00
  10. 8 bush 1 0.40
  11. 9 flower 5 1.00
  12. 10 flower 1 0.20
  13. 11 flower 5 0.90
  14. 12 flower 1 0.15

I want my x axis to compare the age factor 5 and 1, while having the type factor next to each other.

  1. plot = ggplot(data = df, aes(x = age, y = size, fill=type))
  2. plot = plot + geom_col(position = "dodge")
  3. plot

so far so good

Now I'm really struggling with applying sensibles error bars to my columns using geom_errorbar(). They were supposed to display the standard deviation for each column

答案1

得分: 0

这是您想要的吗?其中误差线表示平均值加减一个标准差。

  1. library(dplyr)
  2. library(ggplot2)
  3. df1 <- data.frame(type=as.factor(c("tree","tree","tree","tree", "bush","bush","bush", "bush","flower", "flower", "flower", "flower")),
  4. age=as.factor(c(5,1,5,1,5,1,5,1,5,1,5,1)),
  5. size=c(11,3,16,5,2,0.5,3,0.4,1,0.2,0.9,0.15))
  6. df2 <-
  7. df1 %>%
  8. group_by(type, age) %>%
  9. summarise(sd = sd(size),
  10. mean_size = mean(size), .groups = "drop") %>%
  11. mutate(sd_min = mean_size - sd,
  12. sd_max = mean_size + sd)
  13. ggplot(df2, aes(age, mean_size, fill = type))+
  14. geom_col(position = position_dodge())+
  15. geom_errorbar(aes(ymin = sd_min, ymax = sd_max), position = position_dodge())

如何为使用ggplot2显示均值的柱状图添加单独的误差线。

于2023年06月29日使用 reprex v2.0.2 创建

英文:

Is this what you had in mind? where the error bar represents plus and minus one standard deviation from the mean?

  1. library(dplyr)
  2. library(ggplot2)
  3. df1 &lt;- data.frame(type=as.factor(c(&quot;tree&quot;,&quot;tree&quot;,&quot;tree&quot;,&quot;tree&quot;, &quot;bush&quot;,&quot;bush&quot;,&quot;bush&quot;, &quot;bush&quot;,&quot;flower&quot;, &quot;flower&quot;, &quot;flower&quot;, &quot;flower&quot;)),
  4. age=as.factor(c(5,1,5,1,5,1,5,1,5,1,5,1)),
  5. size=c(11,3,16,5,2,0.5,3,0.4,1,0.2,0.9,0.15))
  6. df2 &lt;-
  7. df1 |&gt;
  8. group_by(type, age) |&gt;
  9. summarise(sd = sd(size),
  10. mean_size = mean(size), .groups = &quot;drop&quot;) |&gt;
  11. mutate(sd_min = mean_size - sd,
  12. sd_max = mean_size + sd)
  13. ggplot(df2, aes(age, mean_size, fill = type))+
  14. geom_col(position = position_dodge())+
  15. geom_errorbar(aes(ymin = sd_min, ymax = sd_max), position = position_dodge())

如何为使用ggplot2显示均值的柱状图添加单独的误差线。<!-- -->

<sup>Created on 2023-06-29 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月29日 17:54:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579963.html
匿名

发表评论

匿名网友

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

确定