Working with ggplot, version 2
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In my previous post on this issue, I had presented a code that made weighted boxplots and annotated them with boxplot statistics and the mean values. The problem with that code was that it printed these annotations right on the vertical axes of the boxplots. Also, a relatively minor problem was that, when the values of two statistics were too close to each other, they were printed one on top of the other.
As the horizontal axis was discrete, ggplot was not able to position the annotations in between two boxplots
With Hadley’s help again, I have managed to fix both of these problems. In the output of the following code, annotations are printed at a fixed distance away from the vertical axis. This is achieved by using grid rather than ggplot to print annotations. With a minor tweak, the distance between annotations is increased if they are too close to each other.
The revised code is as follows.
library(ggplot) vplot2 <- function(dataset,xvar,yvar,v1,v2){ ggopt(axis.colour="black") p <- ggplot(dataset,aesthetics=list(x=x,y=y, weight=Multiplier), colour="black") p$xlabel<-xvar p$ylabel<-yvar (p<-ggpoint(ggboxplot(p,colour="black",orientation="vertical"))) split(dataset,dataset$x)->cl dots <- do.call(rbind, lapply(cl, function(df) { data.frame(x = df[1, ]$x,dots = boxplot_stats_weighted(df$y, weights=df$Multiplier)$stats[3]) })) means<-do.call(rbind, lapply(cl,function(df){ data.frame( x=df[1,]$x, mean=weighted.mean(df$y,df$Multiplier) ) })) (p<-ggpoint(p, data=means, aes=list(x=x, y=mean), colour="magenta")) (pscontinuous(p,variable="y",range=c(v1,v2))->p) print(p,pretty=F) grid.text(format(dots$dots,digits=2),x=unit(as.numeric(dots$x)+0.4, "native"), y=unit(dots$dots,"native"),gp=gpar(col="blue"), vp="layout::panel_1_1") dots$dots->means$dots (means$mean-means$dots)/(v2-v1)->means$diff ifelse(means$diff>=0 & means$diff<0.03,0.03,means$diff)->means$diff ifelse(means$diff<0 & means$diff>-0.03,-0.03,means$diff)->means$diff means$dots+(means$diff*(v2-v1))->means$y grid.text(format(means$mean,digits=2),x=unit(as.numeric(means$x)+0.4, "native"), y=unit(means$y,"native"),gp=gpar(col="magenta"), vp="layout::panel_1_1") }
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.