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

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

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

问题

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

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

现在它看起来是这样的:

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

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

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

到目前为止都很好

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

英文:

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

df=data.frame(type=as.factor(c("tree","tree","tree","tree", "bush","bush","bush", "bush","flower", "flower", "flower", "flower")),
              age=as.factor(c(5,1,5,1,5,1,5,1,5,1,5,1)),
              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:

> df
     type age  size
1    tree   5 11.00
2    tree   1  3.00
3    tree   5 16.00
4    tree   1  5.00
5    bush   5  2.00
6    bush   1  0.50
7    bush   5  3.00
8    bush   1  0.40
9  flower   5  1.00
10 flower   1  0.20
11 flower   5  0.90
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.

plot = ggplot(data = df, aes(x = age, y = size, fill=type))
plot = plot + geom_col(position = "dodge")
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

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

library(dplyr)
library(ggplot2)

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

df2 <- 
  df1 %>%
  group_by(type, age) %>%
  summarise(sd = sd(size),
            mean_size = mean(size), .groups = "drop") %>%
  mutate(sd_min = mean_size - sd,
         sd_max = mean_size + sd)

ggplot(df2, aes(age, mean_size, fill = type))+
  geom_col(position = position_dodge())+
  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?

library(dplyr)
library(ggplot2)

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;)),
              age=as.factor(c(5,1,5,1,5,1,5,1,5,1,5,1)),
              size=c(11,3,16,5,2,0.5,3,0.4,1,0.2,0.9,0.15))

df2 &lt;- 
  df1 |&gt; 
  group_by(type, age) |&gt; 
  summarise(sd = sd(size),
            mean_size = mean(size), .groups = &quot;drop&quot;) |&gt; 
  mutate(sd_min = mean_size - sd,
         sd_max = mean_size + sd)

ggplot(df2, aes(age, mean_size, fill = type))+
  geom_col(position = position_dodge())+
  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:

确定