使用geom_errorbar时数据帧出错?

使用geom_errorbar时数据帧出错?,r,ggplot2,R,Ggplot2,我的数据帧(df)如下所示: df me SD class 3.11 4.08 A 2.09 3.50 B 1.75 2.72 C 0.34 0.85 D 其中me是平均值,SD是标准偏差。我想为每个类别创建这些平均值的条形图,并在这些条形图的顶部添加标准偏差: 要绘制条形图,请执行以下操作: ggplot(data=df) + geom_bar(aes(x=class,y= me), stat="identity",position=

我的数据帧(df)如下所示:

   df
 me  SD class  
 3.11 4.08  A
 2.09 3.50  B
 1.75 2.72  C
 0.34 0.85  D
其中me是平均值,SD是标准偏差。我想为每个类别创建这些平均值的条形图,并在这些条形图的顶部添加标准偏差:

要绘制条形图,请执行以下操作:

ggplot(data=df) +
  geom_bar(aes(x=class,y= me), 
           stat="identity",position="dodge")+ 
  ylab("mean")+ 
  xlab("class")+ 
  theme(
    text = element_text(size=20, colour="black"),  
    axis.text.x = element_text(angle=90, vjust=1, colour="black"), 
    axis.text.y = element_text(colour="black")) + 
  scale_y_continuous(breaks = round(seq(-2, 5, by = 0.5),1))
这个很好用

现在添加错误栏:

ggplot(data=df3) +
  geom_bar(aes(x=class,y= me), 
           stat="identity",position="dodge")+ 
  ylab("mean")+   
  xlab("class")+ 
  theme(
    text = element_text(size=20, colour="black"),  
    axis.text.x = element_text(angle=90, vjust=1, colour="black"), 
    axis.text.y = element_text(colour="black"))+ 
  scale_y_continuous(breaks = round(seq(-2, 5, by = 0.5),1))+ 
  geom_errorbar(aes(ymax = me + SD, ymin= me - SD), 
                position=position_dodge(0.9))
我得到了这个错误:

   Error in data.frame(list(ymin = c(-1.96681604736652, -1.40293149619775,  : 
    arguments imply differing number of rows: 28, 102   

ggplot
中定义
x
y
w:

ggplot(data=df, aes(x = class, y = me)) + 
geom_bar(stat="identity",position="dodge")+ ylab("mean")+   
xlab("class") + theme(text = element_text(size=20, colour="black"),  
axis.text.x = element_text(angle=90, vjust=1, colour="black"), 
axis.text.y = element_text(colour="black"))+ scale_y_continuous(breaks 
= round(seq(-2, 5, by = 0.5),1)) + geom_errorbar(aes(ymax = me + SD, 
ymin= me - SD), position=position_dodge(0.9))

或者:

ggplot(data=df, aes(x = class, y = me)) + 
    geom_bar(stat="identity",position="dodge")+ ylab("mean")+   
    xlab("class") + theme(text = element_text(size=20, colour="black"),  
                          axis.text.x = element_text(angle=90, vjust=1, colour="black"), 
                          axis.text.y = element_text(colour="black"))+ scale_y_continuous(breaks 
                                                                                          = round(seq(-2, 5, by = 0.5),1)) + geom_errorbar(aes(ymax = SD, 
                                                                                                                                               ymin= me - (SD-me)), position=position_dodge(0.9))

它们是按照您在
geom\u errorbar
函数中的定义绘制的。是否要在绘图中包含数字?第二个版本如何?它创建了一个对称范围