使用 geom_smooth 和 ggplot 显示标准差

发布于 2024-10-03 11:07:19 字数 465 浏览 1 评论 0原文

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

例如:

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()

这会生成一个包含两次游程和平滑平均值的图表,但即使两次游程之间的 SD 不断增加,平滑器的条形仍保持相同的大小。我想让平滑器的周围代表给定时间步长的标准偏差。

考虑到许多不同的运行和输出变量,是否有一种非劳动密集型的方法来做到这一点?

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 smoothing.

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()

This 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赤濁 2024-10-10 11:07:19

您好,我不确定我是否正确理解您想要的内容,但例如,

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 的下一版本中。

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.

夏了南城 2024-10-10 11:07:19

如果测量值在 x 上对齐/离散化,则接受的答案才有效。如果是连续数据,您可以使用滚动窗口并添加自定义功能区

iris %>%
    ## apply same grouping as for plot
    group_by(Species) %>%
    ## Important sort along x!
    arrange(Petal.Length) %>%
    ## calculate rolling mean and sd
    mutate(rolling_sd=rollapply(Petal.Width, width=10, sd,  fill=NA), rolling_mean=rollmean(Petal.Width, k=10, fill=NA)) %>%  # table_browser()
    ## build the plot
    ggplot(aes(Petal.Length, Petal.Width, color = Species)) +
    # optionally we could rather plot the rolling mean instead of the geom_smooth loess fit
    # geom_line(aes(y=rolling_mean), color="black") +
    geom_ribbon(aes(ymin=rolling_mean-rolling_sd/2, ymax=rolling_mean+rolling_sd/2), fill="lightgray", color="lightgray", alpha=.8) +
    geom_point(size = 1, alpha = .7) +
    geom_smooth(se=FALSE)

在此处输入图像描述

The accepted answer just works if measurements are aligned/discretized on x. In case of continuous data you could use a rolling window and add a custom ribbon

iris %>%
    ## apply same grouping as for plot
    group_by(Species) %>%
    ## Important sort along x!
    arrange(Petal.Length) %>%
    ## calculate rolling mean and sd
    mutate(rolling_sd=rollapply(Petal.Width, width=10, sd,  fill=NA), rolling_mean=rollmean(Petal.Width, k=10, fill=NA)) %>%  # table_browser()
    ## build the plot
    ggplot(aes(Petal.Length, Petal.Width, color = Species)) +
    # optionally we could rather plot the rolling mean instead of the geom_smooth loess fit
    # geom_line(aes(y=rolling_mean), color="black") +
    geom_ribbon(aes(ymin=rolling_mean-rolling_sd/2, ymax=rolling_mean+rolling_sd/2), fill="lightgray", color="lightgray", alpha=.8) +
    geom_point(size = 1, alpha = .7) +
    geom_smooth(se=FALSE)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文