我想为ggplot放置置信区间误差线。
我有一个数据集,并使用ggplot将其绘制为:
df <- data.frame(
Sample=c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5"),
Weight=c(10.5, NA, 4.9, 7.8, 6.9))
p <- ggplot(data=df, aes(x=Sample, y=Weight)) +
geom_bar(stat="identity", fill="black") +
scale_y_continuous(expand = c(0,0), limits = c(0, 8)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)
p
我是新增错误栏的人。我使用geom_bar查看了一些选项,但无法使其正常工作。
我将在置信区间误差条中放入置信区间的任何帮助表示感谢。谢谢!
用geom_errorbar
添加一层误差线
df <- data.frame(
Sample=c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5"),
Weight=c(10.5, NA, 4.9, 7.8, 6.9))
# add a column containing the SE for each mean
df$SE = 1/1:nrow(df)
ggplot(data = na.omit(df)) + #don't bother plotting the NA
geom_bar(stat = "identity", aes(x=Sample,y=Weight)) +
geom_errorbar(
aes(x=Sample, ymin = Weight - 2*SE, ymax = Weight + 2*SE),
color = "red"
)