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.
ggplot(dt,aes(Time, Value)) +
stat_summary(geom = "line", fun.y = mean) +
stat_summary(geom = "ribbon", fun.data = mean_cl_normal, alpha = 0.3)
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)
Or as alistaire's comment:
ggplot(dt, aes(Time, Value)) +
geom_smooth(stat = 'summary', fun.data = mean_cl_quantile)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With