使用重塑后的数据在 ggplot 中添加误差线

3

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

为了操作ggplot2 y值,我必须将数据框重塑为长格式,因此数据结构如下所示。

这是我的数据的dput():

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")

数据应该长这个样子:
  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

使用ggplot绘制我的数据:
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"))

这样可以生成一个相当吸引人的图形,我能够调整颜色和图例位置,在没有重塑数据的情况下,我很难做到这一点。但我现在面临的问题是如何将错误添加到图表中?
*请注意,变量具有不同数量的NA值,cal5C具有更频繁的NA。
我尝试过:
plot1 <- plot1 + geom_errorbar(data =mydata.m[!is.na(mydata.m$error), ], aes(ymin=mydata.m$value - mydata.m$error, ymax=mydata.m$value + mydata.m$error), width=.05)

但我得到了以下错误:
错误:审美必须是长度为1或与数据相同(32):ymin,ymax,x,y,颜色,组
在这个格式中是否有另一种添加误差线到两个y变量的方法,其中值和误差都不均匀?
提前感谢,希望这讲得清楚。
1个回答

3
您在 geom_errorbar 中的 aes 中引用了完整的数据框,例如 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 = value + error,而不是你所写的 error+ error。
请注意,我没有检查和运行这个代码。在未来,最好使用 dput(mydata.m) 提供你的示例数据,以便其他人更容易地将你的数据(或适当大小的子集)导入 R 进行测试。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接