I am trying to plot two different figures on top of eachother. One figure is built from the full data set, and is plotted as y vs. some categorical variable x. The second data frame is just two values for the means of each categorical x-value in terms of y, and associated standard errors. I can create both plots independently, but I would like to overlap one on top of the other. How can I do this? I have copied code to generate the data, and code for the plots that I wish to overlay on each other here.
#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)
