[R] Gap between axis and bars in barplot()
MSchwartz at MedAnalytics.com
Thu Dec 2 14:55:56 CET 2004
On Thu, 2004-12-02 at 09:30 +0000, michael watson (IAH-C) wrote:
> Windows XP, R 2.0.1.
> I am drawing a very large barplot using jpeg() - setting the width to
> 10,000 as there are over 5000 bars.
> This all works fine and I get exactly what I want - except there is a
> huge bit of white space between the Y-axis and the first bar - so much
> in fact that I have to scroll two screens from the Y-axis before I see
> the first bar. After that the bars are evenly spaced and close
> Any ideas what is going on here?
> Thanks in advance
To use an example to demonstrate the effects, the default barplot is:
Note that in this case, par("usr") which is c(x1, x2, y1, y2) is:
 -239.792 6239.992 -50.000 5000.000
As you note, a fair amount of space on either side of the bars.
Now, specify the x axis style by using 'xaxs = "i"':
barplot(1:5000, xaxs = "i")
Note that par("usr") is now:
 0.2 6000.0 -50.0 5000.0
There is now essentially no space on either side of the bars, as the use
of "i" sets the axis range to exactly the required values for the data,
which in this case is comprised of the bar widths (set to 1 by default)
and the space between the bars (set to 0.2 by default).
In the initial example, the default axis style is "r", which extends the
axis range by 4%. So if you take (5,000 bars * 1) and add (5,000 * 0.2)
for the space, that gives you 6,000 * 1.04, which is 6,240. The
additional 240, is then also subtracted from the minimum x axis value to
provide for symmetric spacing on either side of the bars.
If you want "some space", you can further adjust it by using the 'xlim'
barplot(1:5000, xaxs = "i", xlim = c(-100, 6100))
This increases the x axis range by 100 (instead of 240) on either side
of the bars and you can further adjust to your requirements.
See ?par for more information on adjustments to the axes, etc.
More information about the R-help