首页 > 解决方案 > 如何将标准偏差添加到具有特定测量点的变化图中?

问题描述

我想添加标准错误阴影,如下图所示。 绿色区域是变化范围,灰色区域是标准差,黑色线是平均值。在此处输入图像描述在此处输入图像描述

但是使用我使用的代码,该框无法显示每个测量点。我能做些什么来改进它?

library(ggplot2)

ggplot(df, aes(x=Date, y=Measured))+
      geom_boxplot(aes(Date, Simulated), pch=19, cex=2, ,col="pink")+
      geom_smooth(method = "loess",col="black")+
      scale_y_continuous(limits = c(0,0.45),labels=percent)

在此处输入图像描述

structure(list(Date = structure(c(1511481600, 1520467200, 1522022400, 
                                  1528156800, 1529798400, 1540425600, 1540944000, 1541289600, 1541808000, 
                                  1542067200, 1543881600, 1545004800, 1545264000, 1545523200, 1548288000, 
                                  1551744000, 1552003200, 1552435200, 1557878400, 1558137600, 1558569600, 
                                  NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Simulated = c(0.336166667, 
                                                                                                     0.2475, 0.249, 0.288166667, 0.2835, 0.2635, 0.263333333, 0.267666667, 
                                                                                                     0.270833333, 0.272333333, 0.266833333, 0.2645, 0.340833333, 0.340833333, 
                                                                                                     0.278166667, 0.256333333, 0.3325, 0.3225, 0.184833333, 0.324166667, 
                                                                                                     0.299833333, NA), Measured = c(0.33, 0.262615583275764, 0.24660036402461, 
                                                                                                                                    0.250417899387073, 0.26373249537067, 0.268490464112156, 0.262951031961705, 
                                                                                                                                    0.264276679785603, 0.281844399295363, 0.28668976736686, 0.270193676246981, 
                                                                                                                                    0.273476084624938, 0.323, 0.316, 0.283, 0.271, 0.33, 0.301, 0.317, 
                                                                                                                                    0.286, 0.264, NA)), row.names = c(NA, -22L), class = c("tbl_df", 
                                                                                                                                                                                           "tbl", "data.frame"))

标签: rggplot2

解决方案


添加group = Dategeom_boxplot.

当然,下图没有显示箱线图,因为样本数据中每个日期只有一个测量值。

library(ggplot2)

ggplot(df, aes(x=Date, y=Measured))+
  geom_boxplot(aes(Date, Measured, group = Date) , size = 2)+
  geom_smooth(method = "loess",col="black")
#> Warning: Removed 1 rows containing missing values (stat_boxplot).
#> Warning: Removed 1 rows containing non-finite values (stat_smooth).

reprex 包于 2020-03-16 创建(v0.3.0)


推荐阅读