[R] r: plots

Marc Schwartz MSchwartz at medanalytics.com
Tue Feb 24 15:08:38 CET 2004


On Tue, 2004-02-24 at 06:44, allan clark wrote:
> hi all
> 
> i have another probably simple question.
> 
> I have three variables say x, y and z. x and y are quite large and z is
> relative small.
> how can one plot the three variables on the same graph with two separate
> axis?
> (one for x and y and the other for z)
> 
> e.g.
> x<-c(101,110,150,167,120)
> y<-c(120,135,175,95,200)
> z<-c(0.001, 0.15, 0.6, 0.8, 1)
> 
> regards
> Allan


You could do something like this:

x<-c(101, 110, 150, 167, 120)
y<-c(120, 135, 175, 95, 200)
z<-c(0.001, 0.15, 0.6, 0.8, 1)

# Create a matrix with all three columns
# "Normalize" z against the range of the other two
m <- cbind(x, y, z * 100)

# Now use matplot() to create the plot, which also
# helps to ensure that the y axis covers the range
# of the three vectors
matplot(m, type = "p", pch = c("x", "y", "z"))

# Now annotate axis 4 (the right hand y axis)
# adjusting the labels back to the original z vector 
# scaling. Use axTicks() to get the default tick mark
# locations and divide the values by 100
axis(4, labels = axTicks(4) / 100)

See ?cbind, ?matplot and ?axis and ?axTicks for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list