2016-12-29 87 views
1

我试图平均时间序列数据跨10个分箱并绘制一个分箱平均图。ggplot2中的错误栏stat_summary_bin

使用ggplot2,我怎样才能用stat_summary_bin()覆盖错误栏?

这是我曾尝试:

# My data 
load("xyz.Rdata") 

str(df) 
# data.frame': 125 obs. of 3 variables: 
# $ xdata : num -0.0209 -0.04 1.4145 0.7419 0.9393 ... 
# $ ydata : num -19.78 -23.29 8.86 16.04 11.65 ... 
# $ ir.fac: Factor w/ 3 levels "irh","irl","irm": 2 2 3 3 3 1 3 3 3 2 ... 

# Graph 
ggplot(df) + 
stat_summary_bin(aes(x = xdata, y = ydata, color= ir.fac), 
       fun.y = "mean", 
       bins= 10, size= 0.5, 
       geom= 'line') + 
stat_summary_bin(aes(x = xdata, y = ydata, color = ir.fac), 
       fun.y = "mean", 
       bins= 10, size= 2, 
       geom= "point") 
+2

请让您的示例具有可重现性,以便我们可以运行您的代码。 – eipi10

+1

'fun.data = mean_se'是否提供了您要查找的输出? – eipi10

+0

不,data = mean_se抛出错误。 – ram

回答

1

下面是一个使用内置iris数据帧的例子。默认情况下,fun.data=mean_se给出一个错误栏,等于+/- 1个标准错误。要将其更改为1.96标准错误,请将参数fun.args=list(mult=1.96)添加到stat_summary_bin

library(ggplot2) 
theme_set(theme_classic()) 

pd=position_dodge(0.07) 

ggplot(iris) + 
    stat_summary_bin(aes(x = Sepal.Width, y = Sepal.Length, color= Species), 
        fun.y = "mean", position=pd, 
        bins= 10, size=0.8, 
        geom= "line") + 
    stat_summary_bin(aes(x = Sepal.Width, y = Sepal.Length, color= Species), 
        fun.data = mean_se, position=pd, 
        bins= 10, size= 0.4) 

enter image description here

如果你想有一个自举置信区间,而不是一个传统的标准错误,你可以使用fun.data=mean_cl_boot代替fun.data=mean_se,这将在默认情况下给予95%CI。

+0

谢谢eipi10,帮助表示赞赏 – ram