Josef.Kardos at phila.gov
2009-Jun-17 17:54 UTC
[R] problem with axis alignment when plotting 2 time series on same graph
I am trying to plot 2 time series on the same graph. For example, X1 is the vector of dates and times, its class is POSIXt. Y1 is an environmental parameter, e.g. salinity. X2 is a second vector of dates and times, also of class POSIXt. X2 has a different length than X1, but they have the exact same range. Y2 is another environmental parameter, e.g. stream flow rate. My approach is basically like this: plot(X1,Y1) par(xaxs = "r") plot(X1, Y1) par(new=TRUE) plot(X2, Y2) the result is that the date tick marks for the second graph do not exactly align with the first graph. This is because the second time series is being slightly 'squeezed'; its timescale should match the first graph but for some reason R is plotting it on a smaller scale. The strange thing is that this doesn't happen for all time series I plot. For example X3 and Y3 which have the same basic characteristics as X2 and Y2, overlay perfectly with X1 and Y1. I don't know why it works for some but not others. I also tried setting xaxs to "i" -- and had the same outcome. Also had the same outcome when trying either par(xaxs="i") or par(xaxs="r") after the command par(new=TRUE) Any insights would be appreciated JK [[alternative HTML version deleted]]
Uwe Ligges
2009-Jun-19 17:00 UTC
[R] problem with axis alignment when plotting 2 time series on same graph
Josef.Kardos at phila.gov wrote:> I am trying to plot 2 time series on the same graph. > For example, X1 is the vector of dates and times, its class is POSIXt. Y1 > is an environmental parameter, e.g. salinity. X2 is a second vector of > dates and times, also of class POSIXt. X2 has a different length than > X1, but they have the exact same range. Y2 is another environmental > parameter, e.g. stream flow rate. > > My approach is basically like this: > plot(X1,Y1) > par(xaxs = "r") > plot(X1, Y1) > par(new=TRUE) > plot(X2, Y2)You probably want to use lines() to add the second time series if the dates in X1 and X2 are not of identical range. Uwe Ligges> the result is that the date tick marks for the second graph do not exactly > align with the first graph. This is because the second time series is > being slightly 'squeezed'; its timescale should match the first graph but > for some reason R is plotting it on a smaller scale. > The strange thing is that this doesn't happen for all time series I plot. > For example X3 and Y3 which have the same basic characteristics as X2 and > Y2, overlay perfectly with X1 and Y1. I don't know why it works for some > but not others. > > I also tried setting xaxs to "i" -- and had the same outcome. Also had the > same outcome when trying either par(xaxs="i") or par(xaxs="r") after the > command par(new=TRUE) > > Any insights would be appreciated > > JK > [[alternative HTML version deleted]] > > ______________________________________________ > 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.