[R] Controlling size of boxplot when it is added in a plot

Greg Snow Greg.Snow at intermountainmail.org
Tue Jan 16 22:25:04 CET 2007

One approach is to use the subplot function from the TeachingDemos
package.  I could only get everything to line up properly if I specified
the xlim argument to hist directly and the same value as the ylim
argument to boxplot.

Try this:

> x <- rnorm(100)
> library(TeachingDemos)
> hist(x, breaks = 20, main = NULL, ylim = c(-2,
> subplot( boxplot(x, horizontal=T, ylim=c(-3,3),axes=F ),
par('usr')[1:2], c(par('usr')[3],0))
> box()

I have seen things like this in teaching situations, but not much in the
non-academic world.

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> Charilaos Skiadas
> Sent: Saturday, January 13, 2007 9:12 PM
> To: R-Mailingliste
> Subject: [R] Controlling size of boxplot when it is added in a plot
> Greetings,
> 	I am trying to add a boxplot to the bottom of a 
> histogram, right between the histogram bars and the x axis. 
> Here is the code I am using at the moment (the par line is 
> probably not relevant for our
> discussion):
> hs <- hist(x, breaks = 20, plot = F)
> par(mar = c(3,3,2,1))
> hist(x, breaks = 20, main = NULL, ylim = c(-2, 
> max(hs$counts))) boxplot(x, horizontal = T, axes = T, add = 
> T, at = -1)
> The problem is the following. As it is, the boxplot restricts 
> itself to the -1 line. I would like it to occupy both the -1 
> and the -2 lines ( I guess more generally I would like to 
> control how much vertical space the "embedded boxplot" 
> occupies). I tried to set the width parameter in the boxplot, 
> but that seemed to have no effect at all.
> On an OT note, I haven't seen this way of combining a 
> histogram with a boxplot (perhaps I haven't looked really 
> hard). I thought it would be useful for my students to see 
> them next to each other, to develop a feeling for what 
> histograms might correspond to what boxplots. Is there 
> perhaps some reason why I should avoid showing those graphs 
> to them like that, that I am not aware of? Or just a reason 
> why I haven't seen them combined like this much?
> Charilaos Skiadas
> Department of Mathematics
> Hanover College
> P.O.Box 108
> Hanover, IN 47243
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list