使用 SE 绘制条形图的更简单方法

easier way to graph a barplot with SE

我希望有人可以帮助找到用于生成带有误差条的条形图的快捷方式。总的来说我喜欢这样

# some dummy data
q <- setNames(data.frame(matrix(sample(100,400,T), nrow=20)), 
c("A","B","C","D","E","F","G","H","I","G","K","L","M","O","P","Q","R","S","T","U")) 

#I usually melt the data
library(reshape2)
a1 <- melt(q)

# summarize them
library(plyr)
a2 <- ddply(a1, c("variable"), summarise, mvalue = mean(value, na.rm=TRUE),
                                          medvalue = median(value, na.rm=TRUE),
                                          sd = sd(value, na.rm=TRUE),    
                                          n = sum(!is.na(value)),se = sd/sqrt(n))

#However, I got an error in generating se:
#Error in sd/sqrt(n) : non-numeric argument to binary operator

# then I plot the graph  
library(ggplot2)
ggplot(sum1, aes(x=variable, y=mvalue, fill=variable))+
 geom_bar(stat='identity', position='dodge')+
 geom_errorbar(aes(ymin=mvalue-sd,ymax=mvalue+sd))+
 scale_fill_grey()
 # here i used the sd instead of se 

为什么我收到 se 的错误?有什么方法可以节省所有这些步骤,以更智能的方式生成带有误差条的条形图吗?

ddplydplyr

的阴影下几乎完全不复存在
library(dplyr)
a1$variable <- as.character(a1$variable)
a1  %>% 
  group_by(variable) %>%
  summarise(mvalue = mean(value, na.rm=TRUE),
            medvalue = median(value, na.rm=TRUE),
            sd = sd(value, na.rm=TRUE),    
            n = sum(!is.na(value)), se = sd/sqrt(n)) %>% 
  ggplot(., aes(x=variable, y=mvalue, fill=variable)) +
  geom_bar(stat='identity', position='dodge')+
  geom_errorbar(aes(ymin=mvalue-se, ymax=mvalue+se))+
  scale_fill_grey()