【发布时间】:2017-12-09 01:46:40
【问题描述】:
我正在尝试在 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 轴的范围/限制以包含误差线,但我仍然收到相同的警告消息并且没有误差线。 我希望使用 stat_summary 为多面图生成误差线,而不必再次计算标准误差。在理解为什么分面不允许 stat_summary 正常运行或我做错了什么方面,我们将不胜感激。
【问题讨论】:
标签: r ggplot2 facet summary stat