[R] Multi-line plots with matrices in R

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Mar 7 14:09:35 CET 2007


On Wed, 2007-03-07 at 12:30 +0000, Joseph Wakeling wrote:
> Hello all,
> 
> I'm a new user of R, experienced with Octave/MATLAB and therefore
> struggling a bit with the new syntax.
> 
> One of the easy things in Octave or MATLAB is to plot multiple lines or
>  sets of points by using a matrix where either the columns or the rows
> contain the y-values to be plotted.  Both packages automatically give
> each line/points their own unique colour, character etc.
> 
> I'm wondering how I get the same functionality in R.  For example, if X
> is a vector of x-values and Y is a matrix whose rows contain the
> y-values, I can do,
> 
> apply(Y,1,lines,x=X)

You want maplot here. See ?matplot  but here is an example:

## generate some data to use, a matrix of Y values
## and a vector of x indices.
mat <- matrix(runif(100), ncol = 5)
vec <- seq(1, 100, length = 20)

## plot it using matplot
matplot(vec, mat, type = "l") # type = "l" to get lines

There is also matlines() and matpoints() for adding lines and points to
existing plots.

> 
> ... but of course everything is all in black, with the same type of line
> or points.  I'd like each line to have its own unique colour and/or style.
> 
> Another thing I'd like clarification on is the ability to update an
> existing plot.  For example if I do,
> 
> plot.window(xlim=c(0,100),ylim=c(0,1))

Standard graphics in R are not modifiable after being plotted. You need
to re-plot. When plotting data, I rarely need plot.window. This is what
I would do:

x <- 1:100 * runif(100)
y <- seq(0,1, length = 100) * runif(100)

plot(x, y, xlim = c(0, 100), ylim = c(0, 1))

# now change the limits
plot(x, y, xlim = c(0, 100), ylim = c(0, 0.5))

> 
> and then after plotting data decide I want ylim=c(0,0.5), how do I
> update the graphic?  A new plot.window() command does nothing.

But it does:

opar <- par(mfrow = c(1,2))
plot(x, y, xlim = c(0, 100), ylim = c(0, 0.5))
plot(x, y, xlim = c(0, 100), ylim = c(0, 1))
plot.window(xlim = c(0, 100), ylim = c(0, 0.5))
points(x, y, col = "red")
par(opar)

The points on the left plot correspond exactly to the points in red on
the right plot. The axis limits have changed, but because the axes have
already been labelled, these are not updated. We can illustrate this by
adding axes to the top and right of that plot

opar <- par(mfrow = c(1,2), mar = c(5,4,4,4) + 0.1)
plot(x, y, xlim = c(0, 100), ylim = c(0, 0.5))
plot(x, y, xlim = c(0, 100), ylim = c(0, 1))
plot.window(xlim = c(0, 100), ylim = c(0, 0.5))
points(x, y, col = "red")
axis(3)
axis(4)
par(opar)

Note the changed axis range in the right-hand margin. The problem is
that you can't use plot.window to achieve what you want, not that
plot.window doesn't do anything.

> 
> Many thanks,
> 
>     -- Joe

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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list