Working with ggplot, version 2

[This article was first published on V. » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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.

vplot2 <- function(dataset,xvar,yvar,v1,v2){
  p <- ggplot(dataset,aesthetics=list(x=x,y=y, weight=Multiplier),
  dots <-, lapply(cl, function(df) {
	data.frame(x = df[1, ]$x,dots = boxplot_stats_weighted(df$y,
  means<, lapply(cl,function(df){
  (p<-ggpoint(p, data=means, aes=list(x=x, y=mean), colour="magenta"))
            "native"), y=unit(dots$dots,"native"),gp=gpar(col="blue"),
  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
            "native"), y=unit(means$y,"native"),gp=gpar(col="magenta"),

To leave a comment for the author, please follow the link and comment on their blog: V. » R. 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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)