Skip to content

ylim for graphic

2 messages · Doran, Harold, Marc Schwartz (via MN)

#
On Mon, 2005-08-29 at 12:58 -0400, Doran, Harold wrote:
Harold,

A few thoughts:

1. Instead of fixing the y axis max value at 200, simply set ylim to
c(0, max(math.bar * 1.2)) or a similar constant. In this case, you get
an extra 20% above the max(y) value for the legend placement.


2. In the legend call, use:

  legend("topleft", legend=(c("Label A", "Average")),
          fill = c("blue","orange"))

This will place the legend at the topleft of the plot region, rather
than you having to calculate the x and y coords. If you want it moved in
from the upper left hand corner, you can use the 'inset' argument as
well, which moves the legend by a proportion of the plot region limits
(0 - 1):

    legend("topleft", legend=(c("Label A", "Average")),
          fill = c("blue","orange"), inset = .1)


3. You can use barplot(..., yaxt = "n") to have the y axis not drawn and
then use axis() to place the labels at locations of your choosing, which
do not need to run the full length of the axis range:

 barplot(1:5, yaxt = "n", ylim = c(0, 10))
 axis(2, at = 0:5)


4. You can place the legend outside the plot region, enabling you to
keep the y axis range to <=100. This would need some tweaking, but the
idea is the same:

 # Increase the size of the top margin
 par(mar = c(5, 4, 8, 2) + 0.1)

 # Draw a barplot
 barplot(1:5)

 # Disable clipping outside the plot region
 par(xpd = TRUE)

 # Now draw the legend, but move it up by 30% from the top left
 legend("topleft", legend = LETTERS[1:5], inset = c(0, -.3))


You could also place the legend to the right or left of the plot region
if you prefer, adjusting the above accordingly.

HTH,

Marc Schwartz