David Parkhurst
2014-Feb-02 19:09 UTC
[R] Plotting multiple time series with variables having different units
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? Thank you for any help. David
Bert Gunter
2014-Feb-02 20:33 UTC
[R] Plotting multiple time series with variables having different units
If I understand you correctly, there are so many ways to do this (ggplot2, lattice, base graphics with multiple graphs per page via ?layout,...) that I am puzzled as to how you failed to find what you needed. Exactly what was it that you read that left you unable to do this? Perhaps some additional links need to be created. Incidentally, did you check the CRAN graphics task view? Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch On Sun, Feb 2, 2014 at 11:09 AM, David Parkhurst <parkhurs at imap.iu.edu> 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? > Thank you for any help. > David > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Rolf Turner
2014-Feb-02 20:35 UTC
[R] Plotting multiple time series with variables having different units
Just ***DON'T***!!! Very bad idea; usually wildly misleading. See, e.g.: http://www.perceptualedge.com/articles/visual_business_intelligence/dual-scaled_axes.pdf OTOH if you're going to be bloody-minded and do it anyway, there are brazillions of hits from a Google search which will tell you how. But I repeat: ***DON'T***!!! cheers, Rolf Turner On 03/02/14 08:09, 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?
Gabor Grothendieck
2014-Feb-02 21:38 UTC
[R] Plotting multiple time series with variables having different units
On Sun, Feb 2, 2014 at 2:09 PM, David Parkhurst <parkhurs@imap.iu.edu>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? > Thank you for any help. > David > >library(zoo) dd <- as.Date(c("2000-01-01", "2001-01-01", "2002-01-01")) X <- 1:3 Y <- 4:6 Z <- 7:9 U <- 10:12 z <- zoo(cbind(X, Y, Z, U), dd) plot(z) library(lattice) xyplot(z) library(ggplot2) autoplot(z) + facet_free() -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com [[alternative HTML version deleted]]
Jim Lemon
2014-Feb-02 21:59 UTC
[R] Plotting multiple time series with variables having different units
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?Hi David, 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 of methods. 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. pool_obs<-paste(1,1:12,2007,sep="/") n_ducks<-c(8,5,6,7,3,2,4,5,6,9,8,7) duck_splotches<-c(22,28,23,18,21,19,15,11,12,14,12,16) duck_pool<-data.frame(pool_obs,n_ducks,duck_splotches) duck_pool$pool_obs<-as.Date(duck_pool$pool_obs,"%d/%m/%Y") # the easy way library(plotrix) twoord.plot("pool_obs","n_ducks","pool_obs","duck_splotches", duck_pool, ylab="Number of ducks observed", rylab="Number of duck splotches in pool", xlab="Date",lylim=c(0,30),rylim=c(0,30), xtickpos=duck_pool$pool_obs, xticklab=month.abb, main="Duck splotch throughout the year") You can separate the two series like this: axis.break(2,10,style="gap") You can do what I am presently doing to twoord.plot like this: plot(duck_pool$pool_obs,duck_pool$duck_splotches,ylim=c(0,30), axes=FALSE,ylab="Number of duck splotches in pool",xlab="Date", main="Duck splotch throughout the year",type="b") staxlab(1,at=duck_pool$pool_obs,labels=month.abb) axis(2,at=seq(10,30,by=5)) box() points(duck_pool$pool_obs,duck_pool$n_ducks, type="b",pch=2,col="red") axis(4,at=seq(0,10,by=5),col="red") 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. Jim