2016-07-05 3 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

대신 한 번에 평균과 CI를 만들기 위해'mean_cl_normal'를 사용하지 않는 이유가 있나요? 또한 : 문제의 원인이되는 데이터를 게시 할 수 있습니까 (즉,'data2'가 없으므로 가지고있는 플롯을 만들 수 없습니다). –

+0

죄송합니다. 전에 이것을 놓쳤습니다 : 오류 막대가 격자의 위쪽과 아래쪽 행에서 같은 위치에있는 것으로 보입니다. 가장 큰 원인은 facet이'stat_summary'와'geom_errorbar' (또는 ddply 호출)에서 다르게 작동한다는 것입니다. –

답변

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 

비록 실제 시작 데이터하지 않고, 내가 ddply 것들에 영향을 미치는 방법 data2의 모습, 또는 무엇을 아주 확실하지 않다. 대신에, 나는 모두 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를 사용하는 것을 잊었다는 것입니다! 문제 해결됨! :) 고마워요! –

관련 문제