首页 文章

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

提问于
浏览
0

我正在尝试将误差线添加到具有多个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 回答

  • 1

    您正在引用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 =值错误,而不是你写的错误错误

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

相关问题