刻面时使用 stat_summary 为折线图生成误差条的问题

issue with using stat_summary to produce error bars for line graphs when faceting

我正在尝试使用 ggplot2 中的 stat_summary 将误差线添加到折线图,但是当我对图进行分面时它不起作用

我的数据:

    date week year location imidacloprid block wickhami virescens sexta
1 15-May    1 2015  kinston           tp     1        0         0     0
2 15-May    1 2015  kinston           gh     1        0         0     0
3 15-May    1 2015  kinston          utc     1        0         0     0
4 15-May    1 2015  kinston           gh     2        0         0     0
5 15-May    1 2015  kinston          utc     2        0         0     0
6 15-May    1 2015  kinston           tp     2        0         0     0


'data.frame':   576 obs. of  9 variables:
 $ date        : Factor w/ 27 levels "1-Jul","12-Jun",..: 4 4 4 4 4 4 4 4 4 4 ...
 $ week        : Factor w/ 12 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ year        : Factor w/ 2 levels "2015","2016": 1 1 1 1 1 1 1 1 1 1 ...
 $ location    : Factor w/ 2 levels "kinston","rocky mount": 1 1 1 1 1 1 1 1 1 1 ...
 $ imidacloprid: Factor w/ 3 levels "gh","tp","utc": 2 1 3 1 3 2 3 2 1 2 ...
 $ block       : Factor w/ 4 levels "1","2","3","4": 1 1 1 2 2 2 3 3 3 4 ...
 $ wickhami    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ virescens   : num  0 0 0 0 0 0 0 0 0 0 ...
 $ sexta       : num  0 0 0 0 0 0 0 0 0 0 ...

正在汇总图表数据:

wickhami_sum = summarySE(bug_subset_final, 
                measurevar="wickhami", 
                groupvars=c("imidacloprid","week","year"))

   imidacloprid week year N wickhami         sd          se         ci
1            gh    1 2015 8   0.0000  0.0000000  0.00000000  0.0000000
2            gh    1 2016 8   0.0000  0.0000000  0.00000000  0.0000000
3            gh    2 2015 8   0.0000  0.0000000  0.00000000  0.0000000
4            gh    2 2016 8   0.0000  0.0000000  0.00000000  0.0000000
5            gh    3 2015 8   0.0000  0.0000000  0.00000000  0.0000000
6            gh    3 2016 8   0.1250  0.2314550  0.08183171  0.1935012
7            gh    4 2015 8   0.0000  0.0000000  0.00000000  0.0000000
8            gh    4 2016 8   0.5000  0.4629100  0.16366342  0.3870025
9            gh    5 2015 8   0.5000  0.3779645  0.13363062  0.3159862

下面的代码没有给我任何问题,并生成了我两年数据组合的折线图,并通过 stat_summary

生成误差线
ggplot(wickhami_sum, aes(x=week, y=wickhami,linetype=imidacloprid,group=imidacloprid))+
  stat_summary(fun.data=mean_se,geom="errorbar",width=.2,color="black",position=position_dodge(0.2))+
  stat_summary(fun.y=mean,geom="line",position=position_dodge(0.2))

但是,当我尝试按年份对数据进行分类时(如下所示),我无法stat_summary 生成错误栏并收到以下错误消息

ggplot(wickhami_sum, aes(x=week, y=wickhami,linetype=imidacloprid,group=imidacloprid))+
  stat_summary(fun.y=mean,geom="line",position=position_dodge(0.2))+facet_grid(year~.)+
  stat_summary(fun.data=mean_se,geom="errorbar",width=.2,color="black",position=position_dodge(0.2))

Warning message:
Removed 72 rows containing missing values (geom_errorbar). 

我已经尝试扩展 y 轴的 range/limits 以包含错误栏,但我仍然收到相同的警告消息,但没有错误栏。 我希望使用 stat_summary 为多面图生成误差条,而不必再次计算标准误差。在理解为什么分面不允许 stat_summary 正常运行,或者我没有做正确的事情时,我们将不胜感激。

这是我认为正在发生的事情:未分面图每周有两行数据,但分面图的每个面板每周只有一行数据,导致标准误差计算 return NAstat_summary 适用于未汇总的数据,并在内部进行数据汇总。使用 bug_subset_finalstat_summary,或切换到 geom_errorbar 以继续使用 wickhami_sum。详情如下。

您已经预先汇总了数据,但 stat_summary 旨在处理原始数据并在内部计算汇总值。在您传递给 ggplot 的摘要数据框 wickhami_sum 中,每周有两行,2015 年的每一周和 2016 年的每一周。所有按周和按年的数据都已折叠通过汇总操作将每周和每年缩减为一行。

因此,在未分面的图中,每周有两行数据供 stat_summary 操作。但在多面图中,它试图从单个观察中计算标准误差,这可能是 returning NA,因此没有绘制任何东西。即使在未刻面的图中,您的误差线也是根据每年的两个平均值计算的,这也不是您想要的。

而是继续使用 wickhami_sum,而不是 stat_summary

geom_errorbar(aes(ymin = wickhami - se, ymax=wickhami + se))

或者,使用原始数据(看起来像 bug_subset_final)和 stat_summary:

ggplot(bug_subset_final, aes(x=week, y=wickhami)) +      
  stat_summary(fun.data=mean_se, geom="errorbar)`.