英文:
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
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())
于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 <- 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())
<!-- -->
<sup>Created on 2023-06-29 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论