[英]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. 然后将其转换为ggplot2
可以理解的格式,我们生成了一个简单的图表,显示了var
随着时间的变化。
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. 我想在同一张图中绘制三个var
的平均值,并显示平均值的置信区间。 possibly with +-1SD . 可能带有+ -1SD 。 For this I think the stat_summary()
function can be used, as was outlined here and here . 为此,我认为stat_summary()
函数可以使用,如此处和此处所述 。
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")
If i understood correctly you wanna display average of all three parameters (var0,var1 and var3) with standard deviation. 如果我正确理解,您想显示所有三个参数(var0,var1和var3)的平均值,并带有标准偏差。
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()
: 第一个隐含dplyr
软件包,并按行计算标准偏差和平均值,并使用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)
Second solution imply mentioned by you stat_summary()
, which actually works well with the code you have provided: stat_summary()
暗示了第二个解决方案,它实际上可以与您提供的代码一起很好地工作:
ggplot(data=test_data_long, aes(x=date, y=value)) +
stat_summary(fun.data ="mean_sdl", mult=1, geom = "smooth") + theme_bw()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.