2016-07-05 212 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!问题解决了! :) 谢谢! –