#Christopher J. Mecklin #Murray State University #June 2013 #I hope for this to eventually be a package of functions based #on my teaching and research #functions for drawing ggplot2-style graphs with syntax reminiscent of base require(ggplot2) require(plyr) require(mosaic) boxplot.gg<-function(x,data,xlab=rhs(x),ylab=lhs(x),main=paste("Boxplot of",lhs(x))) { var<-eval(lhs(x),data) grp<-factor(eval(rhs(x),data)) df<-data.frame(grp,var) bp<-ggplot(df,aes(grp,var))+geom_boxplot() if (rhs(x)==1) { bp=bp+theme(axis.text.x=element_blank());xlab=""} bp<-bp+xlab(xlab)+ylab(ylab)+ggtitle(main) return(bp) } ####################################################################### scatterplot.gg<-function(x,data,xlab=rhs(x),ylab=lhs(x),main=paste("Scatterplot of",rhs(x), "versus",lhs(x)) ,line=FALSE,smooth=FALSE) { xvar<-eval(rhs(x),data) yvar<-eval(lhs(x),data) df<-data.frame(xvar,yvar) if (!is.null(condition(x))) { cond<-eval(condition(x),data) df<-data.frame(xvar,yvar,cond) } sp<-ggplot(df,aes(xvar,yvar))+geom_point()+ xlab(xlab)+ylab(ylab)+ggtitle(main) if (!is.null(condition(x))) { sp<-sp+geom_point(aes(colour = factor(cond))) sp<-sp+scale_colour_hue(paste(condition(x))) } if (smooth) sp<-sp+geom_smooth() if (line) sp<-sp+geom_smooth(method=lm) return(sp) } #################################################################### dotplot.gg<-function(x,data,xlab=rhs(x),main=paste("Dotplot of",rhs(x))) { xvar<-eval(rhs(x),data) df<-data.frame(xvar) dp<-ggplot(df,aes(x=xvar))+geom_dotplot() dp<-dp+theme(axis.text.y=element_blank())+xlab(xlab)+ggtitle(main) return(dp) }