This question already has an answer here:
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!
We can use the
stat_summary
as the following way.If you still want the mean with 90 and 10 percentile, you need to design a function return the
y
,ymin
, andymax
of your numerical dataOr as alistaire's comment: