使用geom_smooth和ggplot显示标准偏差 [英] Show standard devation using geom_smooth and ggplot

查看:147
本文介绍了使用geom_smooth和ggplot显示标准偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些数据代表不同场景下的许多模型运行。对于单个场景,我们希望显示平滑的平均值,其中填充区域代表特定时间点的标准偏差,而不是平滑合适的质量。

例如:

  d < -  as.data.frame(rbind(cbind(1:20,1 :20,1),cbind(1:20,-1:-20,2)))
名称(d)< -c(Time,Value,Run)
ggplot(d,aes(x = Time,y = Value))+ geom_line(aes(group = Run))+ geom_smooth()

会生成一个图表,其中包含两个运行表示和一个平滑平均值,但即使运行之间的标准差增加,平滑器的柱状图也保持相同的大小。我希望在给定的时间步使周围的平滑表示标准差。



有没有一种非劳动密集型的方式来做到这一点,因为许多不同的跑步并输出变量?

解决方案

我不知道,如果我正确理解你想要什么,但例如,

  d < -  data.frame(Time = rep(1:20,4),
Value = rnorm(80, rep(1:20,4)+ rep(1:4 * 2,each = 20)),
Run = gl(4,20))

mean_se< - function x,mult = 1){
x < - na.omit(x)
se < - mult * sqrt(var(x)/ length(x))
mean < mean(x)
data.frame(y = mean,ymin = mean -se,ymax = mean + se)
}

ggplot(d,aes(x = Time ,y = Value))+ geom_line(aes(group = Run))+
geom_smooth(se = FALSE)+
stat_summary(fun.data = mean_se,geom =ribbon,alpha = 0.25)

请注意,mean_se将出现在下一个版本离子的ggplot2。


We have some data which represents many model runs under different scenarios. For a single scenario, we'd like to display the smoothed mean, with the filled areas representing standard deviation at a particular point in time, rather than the quality of the fit of smooting.

For example:

d <- as.data.frame( rbind( cbind( 1:20, 1:20,1 ), cbind(1:20, -1:-20,2 ) ) )
names(d)<-c("Time","Value","Run")
ggplot( d, aes(x=Time,y=Value) ) + geom_line( aes(group=Run) ) + geom_smooth()

produces a graph with two runs represented, and a smoothed mean, but even though the SD between the runs is increasing, the smoother's bars stay the same size. I'd like to make the surrounds of the smoother represent standard deviation at a given timestep.

Is there a non-labour intensive way of doing this, given many different runs and output variables?

解决方案

hi i'm not sure if I correctly understand what you want, but for example,

d <- data.frame(Time=rep(1:20, 4), 
                Value=rnorm(80, rep(1:20, 4)+rep(1:4*2, each=20)),
                Run=gl(4,20))

mean_se <- function(x, mult = 1) {  
  x <- na.omit(x)
  se <- mult * sqrt(var(x) / length(x))
  mean <- mean(x)
  data.frame(y = mean, ymin = mean - se, ymax = mean + se)
}

ggplot( d, aes(x=Time,y=Value) ) + geom_line( aes(group=Run) ) + 
  geom_smooth(se=FALSE) + 
  stat_summary(fun.data=mean_se, geom="ribbon", alpha=0.25)

note that mean_se is going to appear in the next version of ggplot2.

这篇关于使用geom_smooth和ggplot显示标准偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆