[R] Boxplot, space to axis

Marc Schwartz MSchwartz at MedAnalytics.com
Thu Sep 30 18:09:30 CEST 2004


On Thu, 2004-09-30 at 10:22, Arne.Muller at aventis.com wrote:
> Hello Deepayan,
> 
> thanks for your suggestion, xaxs='i' works, but it leaves no space at
> all. I though this may be configurable by a real value.
> 
> 	kind regards,
> 
> 	Arne
> 
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch
> > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Deepayan
> Sarkar
> > Sent: 30 September 2004 17:12
> > To: r-help at stat.math.ethz.ch
> > Cc: Muller, Arne PH/FR
> > Subject: Re: [R] Boxplot, space to axis
> > 
> > 
> > On Thursday 30 September 2004 09:41, Arne.Muller at aventis.com wrote:
> > > Hello,
> > >
> > > I've crearted a boxplot with 84 boxes. So fat everything is as I
> > > expect, but there is quite some space between the 1st box and axis
> 2
> > > and the last box and axis 4. Since 84 boxes get very slim anyway
> I'd
> > > like to discribute as much of the horizontal space over the
> x-axis.
> > >
> > > Maybe I've forgotten about a graphics parameter?
> > 
> > Perhaps par(xaxs = "i") ?
> > 
> > Deepayan


Here is a possible solution, albeit a little kludgy.

The problem is that there is no way to define the x axis range in the
call to boxplot(), which would allow you to make some adjustments that
way. If you trace through the code for boxplot() and then bxp() [the
latter actually does the plotting], you could figure out how the range
of the x axis is computed and the factors that influence that
calculation. With that knowledge, you could feasibly adjust the 'at'
argument for the placement of the boxes. However, that may be more
complicated than my approach below.

Create some test data:

set.seed(2)
MyData <- matrix(rnorm(84 * 100), ncol = 84)

and then plot it normally:

boxplot(as.data.frame(MyData))

You can then see the par("usr") values:

> par("usr")
[1] -2.860000 87.860000 -4.379829  3.844485

Note the extra space on the x axis range.


Now, let's control that part of the process:

# Open a new plotting device
plot.new()

# Now specify the plotting coordinate system
# There are 84 boxes, so let's add one unit before
# and after to the 'xlim'. Also, specify 'i' for 
# xaxs, so there is no expansion of the x axis
# range as per the default.
plot.window(xlim = c(0, 85), ylim = range(MyData), xaxs = "i")

# Now do the boxplot, setting 'add = TRUE' so that
# the plot is added to the current window, also
# specifying the 'at' argument
boxplot(as.data.frame(MyData), add = TRUE, at = 1:84)


Now note the par("usr") values:
> par("usr")
[1]  0.000000 85.000000 -4.379829  3.844485


You can do further adjustments to the x axis range as you require in the
call to plot.window().

HTH,

Marc Schwartz




More information about the R-help mailing list