The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Kickstarting R - Plotting more than one data series

Adding points or lines to a plot

If you only want to overlay data series on the same axes, it is sufficient to specify that you don't want to "erase" the first plot and suppress display of the axes after the first plot:
> plot(6:25,rnorm(20),type="b",xlim=c(1,30),ylim=c(-2.5,2.5),col=2)
> par(new=T)
> plot(rnorm(30),type="b",axes=F,col=3)
> par(new=F)

Notice how extra space was left in case the second series had a greater range than the first. The example also shows how to "line up" the series if they don't have the same x range by specifying an explicit X range in the first plot.

If you only want to add series of points/lines that have the same x and y ranges, points() or lines() will do the job

> plot(rnorm(100),type="l",col=2)
> lines(rnorm(100),col=3)

If the first data series plotted has a smaller range than subsequent ones, you will have to include a ylim= argument on the first plot to fit them all in:

> yrange<-range(c(datavector1,datavector2,datavector3))
> plot(datavector1,type="l",ylim=yrange,col=2)
> lines(datavector2,type="l",col=3)
> ...

Dual ordinates

Illustrating the relationship between two things on a plot is a common task. Typically, the abcissa represents time, and the two sets of points (or lines) show some sort of covariation. The trick is getting things lined up so that the relationship between the variables is easy to see. Using artificial data this time, we'll set up two variables that increase and decrease with successive observations. In this case, the two data series have different x and y ranges.

> upvar<-rnorm(10)+seq(1,1.9,by=0.1)
> downvar<-rnorm(20)*5+19:10
> par(mar=c(5,4,4,4))
> plot(6:15,upvar,pch=1,col=3,xlim=c(1,20),xlab="Occasion",ylab="",main="Dual ordinate plot")
> mtext("upvar",side=2,line=2,col=3)

That's the first plot, with green circles showing the data points and a green label on the y axis to identify the ordinate for them. The call to par() adds space for two extra lines on the right side of the plot, which we'll need for the ticks and labels.

> abline(lm(upvar~I(1:10)),col=3)

The I() term in the formula is necessary to prevent lm() from generating an error. Now we've got a plot with green circles and a green line showing the regression of upvar on observation index.

> par(new=T)
> plot(downvar,axes=F,xlab="",ylab="",pch=2,col=4)
> axis(side=4)
> abline(lm(downvar~I(1:20)),col=4)
> mtext("downvar",side=4,line=2,col=4)

First, there's another call to par() that allows us to plot the second set of data without erasing the first plot. The second plot is without axes and labels, so that they don't get mixed up with the first plot. After adding the second axis on the right side and plotting the regression line for downvar, mtext() is used to put the variable name next to the right ordinate. Because we overlaid a new plot, we didn't have to use ylim= to fit the two ranges together. xlim= was used to line up the occasions correctly.

For more information, see An Introduction to R: High level plotting commands.

Back to Table of Contents

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.