I am trying to add error bars to a geom_line() plot which has multiple y values.
To manipulate the ggplot2 y values I had to reshape the data frame in to long format and so the data is structured as shown below.
Here is dput() of my data:
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")
The data should look like this:
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
using ggplot to plot my data:
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"))
This makes a fairly appealing graph where i have been able to adjust colours and legend position, which without reshape i was struggling to do. But the problem i now face is how do i add the error to the graphs?
*Note the variables have different numbers of NA values with cal5C having more frequent NAs.
I have tried:
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)
But I get the following error:
Error: Aesthetics must be either length 1 or the same as the data (32): ymin, ymax, x, y, colour, group
Is there another way to add error bars to both y variables following this format where the values are uneven as are the errors???
Thanks in advance and I hope that makes sense.