[R] Change Axis Size

Gavin Simpson
Fri, 23 Mar 2007 13:13:48 -0000

On Fri, 2007-03-23 at 12:40 +0000, webb at stats.ox.ac.uk
wrote:
> Sorry if this is an obvious question but I have not been able to find the
> answer.
> I wish to plot 3 lines on the same plot. However, whichever one I plot
> first, the axis does not have a big enough range for the other two to be
> shown in the plot (they get cut off at the top and the bottom). Is there a
> way to change the range of the y-axis when adding a new line to the plot?
>
> Thank you,
>
> Alex

Use the ylim parameter in plot():

x <- 1:100
y1 <- sort(runif(100))
y2 <- y1 * 1.2
y3 <- y2 * rnorm(100)
plot(x, y1, type = "n", ylim = range(y1, y2, y3))
lines(x, y1, col = "red")
lines(x, y2, col = "blue")
lines(x, y3, col = "green")

An alternative is matplot:

matplot(x, cbind(y1,y2,y3), col = c("red", "blue",
"green"),
        type = "l", lty = "solid")

or

matplot(x, cbind(y1,y2,y3), type = "n")
matlines(x, cbind(y1,y2,y3), col = c("red", "blue",
"green"),
         lty = "solid")

HTH

G
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Gavin Simpson                 [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%