user08041991 user08041991 - 2 years ago 161
R Question

plotting average with confidence interval in ggplot2 for time-series data

From the following question, we create some dummy data. Then it is converted into a format which

ggplot2
can understand, and we generate a simple graph showing changes in
var
over time.

test_data <-
data.frame(
var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
var2 = 120 + c(0, cumsum(runif(49, -5, 10))),
date = seq(as.Date("2002-01-01"), by="1 month", length.out=100)
)
#
library("reshape2")
library("ggplot2")
#
test_data_long <- melt(test_data, id="date") # convert to long format

ggplot(data=test_data_long,
aes(x=date, y=value, colour=variable)) +
geom_line() + theme_bw()


I want to plot the average of the three
var
in the same graph, and show a confidence interval for the average. possibly with +-1SD. For this I think the
stat_summary()
function can be used, as was outlined here and here.

By adding either of the commands below, I do not obtain the average, nor a confidence interval. Any suggestions would be greatly appreciated.

stat_summary(fun.data=mean_cl_normal)
#stat_summary(fun.data ="mean_sdl", mult=1, geom = "smooth")
#stat_summary(fun.data = "mean_cl_boot", geom = "smooth")

Answer Source

If i understood correctly you wanna display average of all three parameters (var0,var1 and var3) with standard deviation.

I do have for you two solutions. First one imply dplyr package and calculation of the standard deviation and average row-wise and further display using geom_ribbon():

library(dplyr)
library(magrittr)
q <- test_data
q <- q %>% rowwise() %>% transmute(date, mean=mean(c(var0,var1,var2), na.rm=TRUE), sd = sd(c(var0,var1,var2), na.rm=TRUE))

eb <- aes(ymax = mean + sd, ymin = mean - sd)
ggplot(data = q, aes(x = date, y = mean)) + 
  geom_line(size = 2) + 
  geom_ribbon(eb, alpha = 0.5)

enter image description here

Second solution imply mentioned by you stat_summary(), which actually works well with the code you have provided:

ggplot(data=test_data_long, aes(x=date, y=value)) +
  stat_summary(fun.data ="mean_sdl", mult=1, geom = "smooth") + theme_bw()

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download