[R] ggplot2 geom_boxplot limits

Jacob Wegelin jacobwegelin at fastmail.fm
Mon Jul 20 20:26:48 CEST 2015


With base graphics, one can use the "ylim" argument to zoom in on a boxplot.

With ggplot2, using "limits" to try to zoom in on a boxplot *changes the box*.  Since the box usually indicates the 25th and 75th percentiles of a quantitative variable, this is puzzling.

The toy code below demonstrates this. In ggplot2, "zooming in" causes the two boxes to overlap, when they did not overlap in the full plot.  Also, the center lines --- which usually indicate the median of the variable --- change when one zooms in.

In base graphics, "zooming in" does not cause the boxes to overlap or, as far as I can see, the median line to move relative to the scale.

What is going on here?

pdf(file="toy-example.pdf")
set.seed(1)
toy1<-data.frame(Y=rnorm(500, mean=3), A="one")
toy2<-data.frame(Y=rnorm(500, mean=1.6), A="two")
toy<-rbind(toy1,toy2)
toy$A<-factor(toy$A)
library(ggplot2)
mybreaks<-signif(seq(from=min(toy$Y),to=max(toy$Y),by=0.5),digits=2)
mylimits<-c(0.61,3.7)
print(myplot<-ggplot(toy, aes(x=A,y=Y)) + geom_boxplot()+scale_y_continuous(breaks=mybreaks)+theme_bw())
print(myplot+scale_y_continuous(breaks=mybreaks,limits=mylimits))
boxplot(toy1$Y,toy2$Y)
boxplot(toy1$Y,toy2$Y, ylim=mylimits)
graphics.off()

> sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.4 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] ggplot2_1.0.1

loaded via a namespace (and not attached):
  [1] MASS_7.3-40      colorspace_1.2-6 scales_0.2.5     magrittr_1.5     plyr_1.8.3       tools_3.2.1      gtable_0.1.2     reshape2_1.4.1
  [9] Rcpp_0.11.6      stringi_0.5-5    grid_3.2.1       stringr_1.0.0    digest_0.6.8     proto_0.3-10     munsell_0.4.2


Jacob A. Wegelin



More information about the R-help mailing list