[R] Plotting multiple time series with variables having different units
jim at bitwrit.com.au
Sun Feb 2 22:59:48 CET 2014
On 02/03/2014 06:09 AM, David Parkhurst wrote:
> I've tried to figure out how to do this from what I read, but haven't
> been successful. Suppose I have a dataframe with variables Date, X, and
> Y (and maybe U, V, and Z) where X, Y, etc. have different units. I'd
> like to plot Y vs. Time above X vs. Time, above one another.
> For example, X is the number of gulls counted on a reservoir, and Y is
> the number of coliform bacteria counted on a petri plate from a water
> sample leaving the reservoir, so these have very different ranges. U and
> V might be numbers of geese and numbers of ducks counted on the same days.
> What commands would I use to create such a set of plots?
If you did want dual axes, rather than leave you sitting there with your
boss saying, "Why don't you just use Excel, David?", I'll give a couple
One of the bazillion ways mentioned is the twoord.plot function in the
plotrix package. I'm currently working on a way to make it easier to
separate the different values, so I'll give a detailed example. This
example was inspired by my good fortune some years ago to have the use
of an outdoor pool at work in which I swam each morning. There were a
lot of ducks that liked the pool, too.
# the easy way
ylab="Number of ducks observed",
rylab="Number of duck splotches in pool",
main="Duck splotch throughout the year")
You can separate the two series like this:
You can do what I am presently doing to twoord.plot like this:
axes=FALSE,ylab="Number of duck splotches in pool",xlab="Date",
main="Duck splotch throughout the year",type="b")
mtext("Number of ducks observed",side=4,at=6,line=2,col="red")
and of course you can stick in the axis break if you like. You can also
fiddle the values and the two axes so that the two series don't overlap
on the plot even if they do numerically. There is the possibility of
just putting two plots one above the other if you really don't want the
two series on the same plot.
More information about the R-help