2014-11-18 3 views
1

서로 다른 두 개의 그림을 그려 보는 중입니다. 하나의 그림은 전체 데이터 집합에서 작성되며 y와 몇 가지 범주 형 변수 x로 플롯됩니다. 두 번째 데이터 프레임은 y 및 관련 표준 오류로 각 범주 x 값의 평균에 대한 두 값입니다. 독립적으로 두 플롯을 만들 수 있지만, 다른 플롯과 겹치기를 원합니다. 어떻게해야합니까? 나는 데이터를 생성하기위한 코드를 복사하고 여기에 서로 겹치기를 원하는 플롯에 대한 코드를 작성했다.ggplot2의 동일한 그림에 두 개의 다른 데이터 프레임을 그려야합니다.

#first create a data set, called 'data' 
x<-c(1:100) 
a<-rep(0,50) 
b<-rep(1,50) 
class<-c(a,b) 
y<-x*2 + class*20 + rnorm(100,0,3) 
data<-data.frame(x,class,y) 

#then summarize the means and standard errors of that data by the grouping 'class', using  the meanerr function I have scripted here 
meanerr<- function(data,param,grouping){ 
    means<-aggregate(param~grouping,data=data,FUN=mean) 
    sd<-aggregate(param~grouping,data=data,FUN=sd) 
    count<-aggregate(param~grouping,data=data,FUN=length) 
    err<-sd$param/sqrt(count$param) 
    output<-cbind(means,err) 
    return(output) 
} 

means<- meanerr(data,y,class) 


#plot 1- all the points by class 
ggplot(data,aes(x=class,y=y))+geom_jitter(alpha=0.2,position=position_jitter(width=.1)) 

#plot 2- the means and standard errors by class 
ggplot(means,aes(x=grouping,y=param))+geom_point()+geom_errorbar(aes(ymin=param-err,ymax=param+err),width=0.1) 

답변

2

방법이

ggplot()+geom_errorbar(data=means,aes(x=factor(grouping),ymax=param+err,ymin=param-err),width=.2) 
+geom_point(data=data,aes(x=factor(class),y=y),position='jitter',alpha=.4) 

enter image description here

+0

완벽한에 대한! 고맙습니다. – colin

관련 문제