geom_errorbar中二进制的非数字参数

时间:2016-07-12 11:33:55

标签: r ggplot2 errorbar

我试图将误差线添加到直方图中(仅在某些条形图中),但我遇到了问题,我无法解决这个问题。这是我的数据集示例:

      dput(subdat)
      structure(list(mod = c("MME_RCP85", "MME_piControl", "MME_RCP85", 
      "MME_piControl", "MME_RCP85", "MME_piControl", "MME_RCP85",   "MME_piControl", 
      "MME_RCP85", "MME_piControl", "MME_RCP85", "MME_piControl", "MME_RCP85", 
       "MME_piControl", "MME_RCP85", "MME_piControl", "MME_RCP85", "MME_piControl", 
      "MME_RCP85", "MME_piControl", "MME_RCP85", "MME_piControl"), 
      id = c(4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 
      5L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L), variable = structure(c(1L, 
      1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 
      9L, 9L, 10L, 10L, 11L, 11L), .Label = c("A", "C", "E", "N", 
     "NE", "NW", "S", "SE", "SW", "U", "W"), class = "factor"), 
      value = c(23.3739104936128, 20.1308610498121, 18.8255024990622, 
      19.0964172156593, 5.77035863185792, 5.55522907864065, 7.7881775809746, 
      6.79307018295126, 4.32731156887715, 3.81988029851766, 7.39932393157495, 
      6.53369517479574, 7.22600735346051, 7.25113200310887, 3.09683685988686, 
      3.20062134399185, 5.97626752214253, 5.53675529950584, 5.86005136849718, 
      4.81357821706405, 10.3562524093597, 9.26876021032058), std = list(
      0, 1.1044752690746, 0, 0.985464166294808, 0, 1.28014680316859, 
      0, 1.26851693389212, 0, 1.31353120434552, 0, 1.04172687591445, 
      0, 2.37424970962826, 0, 1.27746257356022, 0, 2.46069388335777, 
      0, 1.31748174778294, 0, 1.64599561125564)), .Names = c("mod", 
      "id", "variable", "value", "std"), row.names = c(4L, 5L, 9L, 
      10L, 14L, 15L, 19L, 20L, 24L, 25L, 29L, 30L, 34L, 35L, 39L, 40L, 
      44L, 45L, 49L, 50L, 54L, 55L), class = "data.frame")

我的情节如下:

p  <- ggplot(subdat,aes(variable,value,fill=mod))  
p +geom_bar(stat="identity",position='dodge')
p +    geom_errorbar(aes(ymin=value-std, ymax=value+std), width=.2,
                       position=position_dodge(.9))

我收到错误:

  

值出错 - std:二元运算符的非数字参数

我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:3)

您需要更改std的类:

enter image description here

subdat$sd = as.numeric(subdat$std)

p  <- ggplot(subdat,aes(variable,value,fill=mod))  
p <- p + geom_bar(stat="identity",position='dodge')
p + geom_errorbar(aes(ymin = value - sd, ymax = value + sd), width = 0.2, position = position_dodge(0.9))