[R] ggplot2 boxplot: horizontal, univariate

Jacob Wegelin jacobwegelin at fastmail.fm
Fri Jun 18 16:47:46 CEST 2010


In ggplot2, I would like to make a boxplot that has the following properties:

(1) Contrary to default, the meaningful axis should be the horizontal axis.

Lattice does this, for instance, by

library(lattice);bwplot(~mtcars$mpg)

(2) It is *univariate*, i.e., of a single vector, say mtcars$mpg.  I do not wish to make separate plots for the different values of mtcars$cyl.

(3) Nothing on the meaningless axis--the axis that does not correspond to values of mtcars$mpg, the axis which by default is horizontal--should suggest a scale. Thus, there should be no axis title or label; there should be no axis ticks.

Partial solutions:

To achieve (1), one might save a vertical plot (with mpg on the y-axis) as a pdf and use Adobe Acrobat to rotate it 90 degrees. But is there not a way to do this *within* ggplot2?

Since ggplot2 has been carefully thought out starting with the grammar of graphics, I wonder if there is some conceptual argument against making univariate boxplots and against boxplots with a horizontal continuous axis.

But in teaching an introductory statistics course, I would like to compare the ways that a histogram and a boxplot summarize a single continuous variable. Thus I would like the continuous axis to be horizontal in both plots.

The following code achieves (2) and part of (3):

library(ggplot2)

qplot(factor(0), mpg, data=mtcars, geom="boxplot", xlab="")+ opts(axis.text.x = theme_blank(), axis.ticks=theme_blank())

but to remove the ticks from the meaningless (horizontal) axis I had to also remove the ticks from the meaningful (vertical) axis.  On page 143 of Wickham's ggplot2 book, I find axis.ticks as a theme element but nothing like axis.ticks.x.

Is there a way to remove the axis ticks from the x axis and not from the y axis?

But, more important: How do I make a boxplot that is rotated (or transposed) from the default, so that the x axis carries the information?

Thanks

Jacob A. Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
730 East Broad Street Room 3006
P. O. Box 980032
Richmond VA 23298-0032
U.S.A.



More information about the R-help mailing list