使用重新整形的数据向ggplot添加错误栏

时间:2018-01-16 04:18:41

标签: r ggplot2 reshape2 errorbar

我正在尝试将误差线添加到具有多个y值的geom_line()图中。

要操纵ggplot2 y值,我必须将数据帧重新整形为长格式,因此数据的结构如下所示。

这是我的数据的dput():

With cmd.Parameters
     .Add("@PLAASNO", SqlDbType.VarChar, 50, "F1")
     '.Add("@NAME1", SqlDbType.VarChar, 50, "F2")         'Remove this line
     .Add("@BLOKNO", SqlDbType.VarChar, 50, "F3")
     '.Add("@NAME2", SqlDbType.VarChar, 50, "F4")         'Remove this
     '.Add("@ANALISEDATUM", SqlDbType.VarChar, 50, "F7")  'Remove
     '.Add("@NAME3", SqlDbType.VarChar, 50, "F8")         'Remove
     .Add("@DATUM", SqlDbType.Date, 50, "F9")
     .Add("@SUIKER", SqlDbType.Decimal, 50, "F10")
     .Add("@SUUR", SqlDbType.Decimal, 50, "F12")          'SUUR before pH
     .Add("@pH", SqlDbType.Decimal, 50, "F11")            'pH is last
End With

数据应如下所示:

mydata.m <- structure(list(Date = structure(c(16968, 16969, 16970, 16971, 
                                   16972, 16973, 16974, 16975, 16968, 16969, 16970, 16971, 16972, 
                                   16973, 16974, 16975), class = "Date"), error = c(NA, 4e-04, NA, 
                                                                                    0.0085, 0.0106, 0.179, NA, 0.0065, NA, 6e-04, NA, 0.007, NA, 
                                                                                    0.0129, NA, NA), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                            1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("c", "cal5C"
                                                                                                                            ), class = "factor"), value = c(NA, 0.0065, NA, 0.0625, 0.089, 
                                                                                                                                                            0.1825, NA, 0.1299, NA, 0.0046, NA, 0.082, NA, 0.16, NA, NA)), .Names = c("Date", 
                                                                                                                                                                                                                                      "error", "variable", "value"), row.names = c(NA, -16L), class = "data.frame")

使用ggplot绘制我的数据:

  head(mydata.m)
        Date  error variable  value
1 2016-06-16     NA        c     NA
2 2016-06-17 0.0004        c 0.0065
3 2016-06-18     NA        c     NA
4 2016-06-19 0.0085        c 0.0625
5 2016-06-20 0.0106        c 0.0890
6 2016-06-21 0.1790        c 0.1825

这是一个相当吸引人的图表,我已经能够调整颜色和图例位置,没有重塑我正在努力做。但我现在面临的问题是如何将错误添加到图表中?

*注意变量具有不同数量的NA值,其中cal5C具有更频繁的NA。

我试过了:

plot1 <- ggplot(mydata.m[!is.na(mydata.m$value), ], 
            aes(x=Date, y=value, color=variable, group = variable))
plot1 <- plot1 + geom_point(size=8) + geom_line(linetype = 6, lwd =1.5)               

plot1 <- plot1 + scale_color_manual(name="", values = 
                                  c("navyblue","turquoise3"), labels = c("C", "calC")) 

plot1 <- plot1+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                  panel.background = element_blank(), axis.line = element_line(colour = "black"))

break.vec <- c(as.Date("2016-06-16"),
           seq(from=as.Date("2016-06-16"), to=as.Date("2016-06-23"), by="day"))

plot1 <- plot1 + scale_x_date(breaks = break.vec, date_labels = "%d-%m",expand = c(0.05,0))

plot1 <- plot1 + theme(text = element_text(size=25), axis.text.x = element_text(size=35), 
                   axis.title.x = element_text(size=45), 
                   axis.title.y = element_text(size=45,margin=margin(t=0,r=20,b=0,l=0)),
                   axis.text.y = element_text(size=35))

plot1 <- plot1 + theme(legend.justification = c(1, 1), legend.position = c(0.05, 1), legend.key = element_rect(fill = "white"))

但是我收到以下错误:

错误:美学必须是长度1或与数据(32)相同:ymin,ymax,x,y,color,group

是否有另一种方法可以在这两种y变量之后添加错误条,其中值不均匀,错误也是如此?

提前致谢,我希望这是有道理的。

1 个答案:

答案 0 :(得分:1)

您正在引用geom_errorbar的完整数据框,例如mydata.m$error,即使您已告知它使用缩减的数据帧。你应该只是引用列名

plot1 <- plot1 + geom_errorbar(data =mydata.m[!is.na(mydata.m$error), ], aes(ymin=value - error, ymax=value + error), width=.05)

我还假设你的意思是ymax =值+错误,而不是你写的错误+错误

请注意我还没有检查并运行它。将来最好用dput(mydata.m)提供您的示例数据,以便其他人更容易将您的数据(或适当大小的子集)放入R进行测试。