Charilaos Skiadas
2007-Jan-14 04:12 UTC
[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? TIA Charilaos Skiadas Department of Mathematics Hanover College P.O.Box 108 Hanover, IN 47243
Chuck Cleland
2007-Jan-14 10:50 UTC
[R] Controlling size of boxplot when it is added in a plot
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
Charilaos Skiadas
2007-Jan-14 21:33 UTC
[R] Controlling size of boxplot when it is added in a plot
Sorry, meant to send this to the list. On Jan 14, 2007, at 4:28 PM, Charilaos Skiadas wrote:> On Jan 14, 2007, at 5:50 AM, Chuck Cleland wrote: > >> Try setting the boxwex argument instead: > > Thanks Chuck, that does indeed seem to work pretty well. I'm not > quite sure what the best way to determine an appropriate size for > the boxplot would be, but the following kind of works, at least for > the cases I tried. Though I'm not entirely happy with it. And I'm > sure I've made a bunch of errors along the way, that someone more > experienced in R could spot easily. Feel free to criticize the > code. the boxwex default I guess is probably terribly named. One > over it is supposed to be the size of the boxplot over the size of > the histogram. > > force.odd <- function(x) { > x + 1 - x %% 2; > } > boxhist <- function(x, boxwex = 8, ...) { > hs <- hist(x, breaks = 20, plot = F) > space <- force.odd(max(floor(hs$counts / boxwex), 1)) > plot(hs, main = NULL, ylim = c(-space, max(hs$counts)), ...) > boxplot(x, horizontal = T, axes = T, add = T, at = -space/2, > boxwex = space) > } > x <- rweibull(300,1,1); boxhist(x) > > HarisHaris
Martin Maechler
2007-Jan-15 13:08 UTC
[R] Controlling size of boxplot when it is added in a plot
>>>>> "Charilaos" == Charilaos Skiadas <skiadas at hanover.edu> >>>>> on Sat, 13 Jan 2007 23:12:00 -0500 writes:Charilaos> I am trying to add a boxplot to the bottom of a Charilaos> histogram, right between the histogram bars and Charilaos> the x axis. Here is the code I am using at the Charilaos> moment (the par line is probably not relevant for Charilaos> our discussion): Charilaos> hs <- hist(x, breaks = 20, plot = F) Charilaos> par(mar = c(3,3,2,1)) Charilaos> hist(x, breaks = 20, main = NULL, ylim = c(-2, max(hs$counts))) Charilaos> boxplot(x, horizontal = T, axes = T, add = T, at = -1) Charilaos> The problem is the following. Charilaos> ........................ Charilaos> On an OT note, I haven't seen this way of Charilaos> combining a histogram with a boxplot (perhaps I Charilaos> haven't looked really hard). I thought it would Charilaos> be useful for my students to see them next to Charilaos> each other, to develop a feeling for what Charilaos> histograms might correspond to what boxplots. Is Charilaos> there perhaps some reason why I should avoid Charilaos> showing those graphs to them like that, that I am Charilaos> not aware of? Or just a reason why I haven't seen Charilaos> them combined like this much? The 'sfsmisc' package (on CRAN) has been containing a function called hist.bxp() which does these plots, for a very long time --- actually for almost a longer time than R exists. It was part of our internal collection of S-plus "goodies" since 1995. Martin Maechler, ETH Zurich
Greg Snow
2007-Jan-16 21:25 UTC
[R] Controlling size of boxplot when it is added in a plot
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,max(hs$counts)),xlim=c(-3,3))> 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? > > 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. >