2016-12-29 90 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