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

Chuck Cleland ccleland at optonline.net
Sun Jan 14 11:50:40 CET 2007


Charilaos Skiadas wrote:
> 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.

  Try setting the boxwex argument instead:

par(mfrow=c(1,2))

set.seed(54321)
x <- rnorm(100)
hs <- hist(x, breaks = 20, plot = F)
hist(x, breaks = 20, main = NULL, ylim = c(-2, max(hs$counts)))
boxplot(x, horizontal = T, axes = T, add = T, at = -1, boxwex = 1)

hist(x, breaks = 20, main = NULL, ylim = c(-2, max(hs$counts)))
boxplot(x, horizontal = T, axes = T, add = T, at = -1, boxwex = 3)

> 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?
> 
> TIA
> 
> 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. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list