如果结果变量在R中使用ggplot是二进制的,如何在条形图中添加误差线?

时间:2017-12-18 23:42:03

标签: r ggplot2 errorbar

我试图使用二元结果变量(' cr')来绘制因子('情绪' 3级)的主效应中的

#computing mean and sd
    data_summary <- function(data, varname, groupnames){
      require(plyr)
      summary_func <- function(x, col){
        c(mean = mean(x[[col]], na.rm=TRUE),
          sd = sd(x[[col]], na.rm=TRUE))
      }
      data_sum<-ddply(data, groupnames, .fun=summary_func,
                      varname)
      data_sum <- rename(data_sum, c("mean" = varname))
      return(data_sum)
    }

     sophie1 <- data_summary(sophie, varname="cr", groupnames ="emotion") 
     head(sophie1) 



    base_plot <- ggplot(sophie, aes(x = emotion, y = cr)) +
      stat_summary(aes(fill = factor(emotion)), fun.y = mean, geom = "bar", color = "black") + 
      scale_fill_manual(values = c("#3182bd", "#636363", "#e6550d")) +  # colorbrewer2.org
      theme(legend.position = "none") +
      theme_bw() +
      xlab("Emotions") + ylab("Pr (correct response)")+
      geom_errorbar(aes(ymin=cr-sd, ymax=cr+sd), width=.2,
                    position=position_dodge(.9)) +
      theme(panel.grid.major = element_blank())
    print(base_plot)

我收到此错误消息:

Error in cr - sd : non-numeric argument to binary operator

感谢您的帮助。

0 个答案:

没有答案