2016-07-05 267 views
0

我正在嘗試使用多行和stat_summary來定義平均值。當我應用geom_errorbar()時,它們中的一些被放置在距離平均跡象一定距離處,這意味着其中一些是「飛行」的。發生什麼事?錯誤位置錯誤

謝謝!

我的代碼:

#First I add another data set with SE, SD and mean. 
cdata <- ddply(data2, c("OGTT","Treatment"), summarise, 
       N = sum(!is.na(Glucose)), 
       mean = mean(Glucose, na.rm=TRUE), 
       sd = sd(Glucose, na.rm=TRUE), 
       se = sd/sqrt(N)) 


    #Then I merge it with my original data 
totalglu<-merge(data2,cdata) 

#Then I make the ggplot 
p<-ggplot(data=totalglu, aes(x = factor(OGTT), y = Glucose, group = StudyID, color=StudyID)) + 
    geom_line() + 
    facet_grid(End.start ~Treatment)+ 
    stat_summary(aes(group = Treatment), geom = "point", fun.y = mean, shape = 16, size = 2) + 
    theme(legend.position="none") + 
    labs(x = "OGTT time points (min)",y= "Glucose (mmol/l)")+ 
    geom_errorbar(aes(ymin=mean-se,ymax=mean+se), width=.1, colour="black") 
p 

My plot with flying errorbars for a some of the points

+0

有什麼理由不使用'mean_cl_normal'使一次性代替均值和CI?另外:你能發佈導致問題的數據嗎(也就是說,我沒有'data2',所以不能繪製你的圖)。 –

+0

對不起,我之前錯過了這一點:看起來,錯誤欄位於網格的頂部和底部行中的相同位置。最有可能的原因是在'stat_summary'和'geom_errorbar'(或在ddply調用)中,facet的工作方式不同 –

回答

0

看來,在計算的時候吧,你不使用End.start,但它正在由stat_summary因爲小面的。

嘗試:

cdata <- ddply(data2, c("OGTT","Treatment","End.start"), summarise, 
       N = sum(!is.na(Glucose)), 
       mean = mean(Glucose, na.rm=TRUE), 
       sd = sd(Glucose, na.rm=TRUE), 
       se = sd/sqrt(N)) 


    #Then I merge it with my original data 
totalglu<-merge(data2,cdata) 

#Then I make the ggplot 
p<-ggplot(data=totalglu, aes(x = factor(OGTT), y = Glucose, group = StudyID, color=StudyID)) + 
    geom_line() + 
    facet_grid(End.start ~Treatment)+ 
    stat_summary(aes(group = Treatment), geom = "point", fun.y = mean, shape = 16, size = 2) + 
    theme(legend.position="none") + 
    labs(x = "OGTT time points (min)",y= "Glucose (mmol/l)")+ 
    geom_errorbar(aes(ymin=mean-se,ymax=mean+se), width=.1, colour="black") 
p 

雖然,沒有實際的起始數據,我不太知道什麼data2樣子,或如何ddply是影響的事情。相反,我可能會建議跳過製作cdata乾脆,只是使用:

ggplot(data=totalglu, aes(x = factor(OGTT), y = Glucose, group = StudyID, color=StudyID)) + 
    geom_line() + 
    facet_grid(End.start ~Treatment)+ 
    stat_summary(aes(group = Treatment), fun.data = mean_cl_normal) + 
    theme(legend.position="none") + 
    labs(x = "OGTT time points (min)",y= "Glucose (mmol/l)") 
+0

非常感謝! :)這非常有幫助!正如你所說,我的問題是我在計算酒吧時忘了使用End.start!問題解決了! :) 謝謝! –