Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot time series with ggplot with confidence interval [duplicate]

Tags:

r

ggplot2

If I have a data table with a time series in which every time stamps have multiple observation, is there a direct way to plot that data set with the mean and interval?

For example, creating the data set:

dt <- lapply(seq(1,10),function(x) {
  dt <- data.table(Time = seq(1,100),
                   Value = seq(1,100)* 3 + rnorm(100,5,20))
})

dt <- rbindlist(dt,idcol = 'Run') 

ggplot(dt,aes(Time,Value,group = Run)) +
  geom_line(size = 0.1,alpha = 0.5)

Each time stamp has multiple observations. What I want the plot to look like is something like this:

ggplot(dt[,list(Value = mean(Value),
                MaxValue = quantile(Value, 0.9),
                MinValue = quantile(Value, 0.1)),
          list(Time)])+
  aes(x = Time, y = Value,ymin = MinValue,ymax = MaxValue)+
  geom_line()+
  geom_ribbon(alpha = 0.3)

This works, but seems like a lot of lines for something that should be simpler. For example, if I was doing boxplot, I could do this in a much simpler ggplot call:

ggplot(dt)+
  aes(x = factor(Time), y = Value)+
  geom_boxplot()

Thank you for the help!

like image 992
Leo Barlach Avatar asked Aug 23 '18 19:08

Leo Barlach


1 Answers

We can use the stat_summary as the following way.

ggplot(dt,aes(Time, Value)) +
  stat_summary(geom = "line", fun.y = mean) +
  stat_summary(geom = "ribbon", fun.data = mean_cl_normal, alpha = 0.3)

enter image description here

If you still want the mean with 90 and 10 percentile, you need to design a function return the y, ymin, and ymax of your numerical data

mean_cl_quantile <- function(x, q = c(0.1, 0.9), na.rm = TRUE){
  dat <- data.frame(y = mean(x, na.rm = na.rm),
                    ymin = quantile(x, probs = q[1], na.rm = na.rm),
                    ymax = quantile(x, probs = q[2], na.rm = na.rm))
  return(dat)
}

ggplot(dt,aes(Time, Value)) +
  stat_summary(geom = "line", fun.y = mean) +
  stat_summary(geom = "ribbon", fun.data = mean_cl_quantile, alpha = 0.3)

enter image description here

Or as alistaire's comment:

ggplot(dt, aes(Time, Value)) + 
  geom_smooth(stat = 'summary', fun.data = mean_cl_quantile)

enter image description here

like image 183
www Avatar answered Oct 17 '22 07:10

www