[R] boxplot and xlim confusion?

Marc Schwartz (via MN) mschwartz at mn.rr.com
Thu Sep 29 16:34:06 CEST 2005

On Thu, 2005-09-29 at 15:28 +0200, Karin Lagesen wrote:
> "Petr Pikal" <petr.pikal at precheza.cz> writes:
> > Hi Karin
> >
> > I did not have seen any answer for your question yet so here is a 
> > try.
> >
> > I gues you want the horizotal layout or your boxplot.
> >
> > boxplot(split(rnorm(30), rep(1:3, each=10)), horizontal =T, 
> > names=letters[1:3])
> >
> > boxplot(split(rnorm(30), rep(1:3, each=10)), horizontal =T, 
> > names=c(NA,"b",NA))
> These look like the plots I want, yes. However, is there a way of
> locking the x-axis? When I do these several times in a row, the range
> of the x axis moves with the data being plotted. If I set xlim in the
> boxplot command, it makes no difference what so ever.

That's because when you use 'horizontal = TRUE' in boxplot(), the x and
y axes are rotated. Thus you need to set 'ylim' and not 'xlim', where
ylim is the range of values in your data irrespective of the axis

This is done in bxp(), which is the actual function doing the plotting
for boxplot(). In bxp(), there is the follow code snippet:

        if (horizontal) 
            plot.window(ylim = c(0.5, n + 0.5), xlim = ylim, 
                        log = log)
        else plot.window(xlim = c(0.5, n + 0.5), ylim = ylim, 
                         log = log)

Note in the first case, that the xlim value for plot.window() is set to
the ylim value passed from boxplot() or from bxp(), if called directly.

So, the above examples by Petr should be something like:

boxplot(split(rnorm(30), rep(1:3, each=10)), horizontal = TRUE, 
        names=letters[1:3], ylim = c(-4, 4))

boxplot(split(rnorm(30), rep(1:3, each=10)), horizontal =TRUE, 
        names=c(NA,"b",NA), ylim = c(-4, 4))


Marc Schwartz

More information about the R-help mailing list