Missing values in boxplots

568 views
Skip to first unread message

Ted Mendum

unread,
Mar 25, 2011, 1:30:21 PM3/25/11
to ggp...@googlegroups.com
Hello,

The following code lifted from
http://had.co.nz/ggplot2/geom_boxplot.html shows a plot about which I
have a question.

p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(aes(fill = factor(vs)))

In the mtcars data there are no cars which have 8 cylinders and a value
of 1 for vs. All the 8 cylinder cars have a value for 0. Because of
this, the boxplot for 8 cylinders, vs=0 is twice as wide as the the rest
of the boxplots.

Is there any way to make the boxplot widths uniform despite the fact
that there are missing values? Ideally, I'd like the mpg data for 8
cylinder, vs=0 cars to be plotted to the left of the gridline for 8
cylinders, and the space to the right of the gridline left blank.

Thanks

Ted Mendum

Kohske Takahashi

unread,
Mar 25, 2011, 11:45:16 PM3/25/11
to Ted Mendum, ggp...@googlegroups.com
Hi,

here is a very very ad-hoc and dirty and clumsy example ...

mtcars2 <- transform(mtcars, visible="black")
d <- rbind(mtcars2, transform(mtcars2[1,], cyl=8, vs=1, mpg=15, visible=NA))
p <- ggplot(d, aes(factor(cyl), mpg)) +
geom_boxplot(aes(fill = factor(vs), colour=visible)) +
scale_colour_identity(legend=FALSE)

is there a better way?

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Brandon Hurr

unread,
Mar 26, 2011, 4:22:55 AM3/26/11
to Kohske Takahashi, Ted Mendum, ggp...@googlegroups.com
Creating a dummy variable is all I could come up with as well, but it took me too long to try to come up with an example. 

It would be nice if there was a setting to preserve the positioning of the boxplots so they don't take up all the space of a factor, the only take up the space that they do when there aren't "missing" data. I'm not sure having it fill the space really makes sense to begin with. 

Brandon 
Reply all
Reply to author
Forward
0 new messages