简体   繁体   English

如何在数据帧和 ggplot2 中添加错误栏?

[英]How can I add a error bar in dataframe and in ggplot2?

I am making a grouped bar graph using ggplot2.我正在使用 ggplot2 制作分组条形图。 However, I am having trouble adding sd/sem.但是,我在添加 sd/sem 时遇到了问题。 The standard errors are: '1,2,3,1'.标准错误是:'1,2,3,1'。 How can I add Error Bar to this bar graph?如何将误差条添加到此条形图中?

survey <- data.frame(group=rep(c("LG", "RM"),each=2),
                sample=rep(c("sample1", "sample2"),1),
                values=c(200,50,300,25 ))


library(ggplot2)
ggplot(survey, aes(x=sample, y=values, fill=group)) + 
 geom_bar(stat="identity", position=position_dodge())

In this case just put the fixed values directly, as they were calculated already somewhere else.在这种情况下,只需直接输入固定值,因为它们已经在其他地方计算过。

survey <- data.frame(group=rep(c("LG", "RM"),each=2),
                     sample=rep(c("sample1", "sample2"),1),
                     values=c(200,50,300,25 ),
                     se=c(1,2,3,1)) #added values here


library(ggplot2)
ggplot(survey, aes(x=sample, y=values, fill=group)) + 
  geom_bar(stat="identity", position=position_dodge())+
  geom_errorbar(aes(ymin=values - se, ymax=values + se),
                position=position_dodge(width = 0.9),width=0.5)

gives:给出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM