# [R] different data series on one graph

Anne York york at noaa.gov
Wed May 1 16:24:28 CEST 2002

```Here is a function that can be easily modified for your purposes.

Example of usage:

x2 <- 0:12
y1 <- x1^2
y2 <- x2+3

plot.double(x1,y1,x2,y2,lty1=1,lty2=3,pch2=1,type1="b",type2="h")

"plot.double" <- function(x1, y1, x2, y2, type1 = "l",type2="l", xlim =
NULL,   main = NULL, xlab = NULL,lty1=1,lty2=2,pch2=16,pch1=1,
ylab1 = "y1", ylab2 = "y2", ...)
{
# Written by AE York Sept. 1996 for Splus; converted to R Jan 2001
# Plots y1 vs x1 and y2 vs x2 on the same plot types type1 and type2
# Labels for y1 are on the left and labels for y2 on the right
oldpar <- par()
par(mar = rep(6, 4))    #
if(missing(xlim))
xlim <- range(pretty(range(c(x1, x2))))
plot(x1, y1, lty = lty1, pch = pch1, type = type1, xlim = xlim,
xlab = xlab, ylab = ylab1, ...)
par(new = T)
plot(x2, y2, lty = lty2, pch = pch2, type = type2, axes = F, xlim =
xlim, xlab  = "", ylab = "")
axis(side = 4)
mtext(ylab2, side = 4, line = 2, outer = F)

title(main=main)

par(mar=oldpar\$mar)

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Anne E. York
National Marine Mammal Laboratory
Seattle WA 98115-0070  USA
e-mail: anne.york at noaa.gov
Voice: +1 206-526-4039
Fax: +1 206-526-6615
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

On Fri, 26 Apr 2002 wouter.buytaert at yucom.be wrote:

>
>Hello,
>
>I'm looking for a way to plot different data series on one graph.
>I have a series of hourly rainfall and quarterly flow
>measurements (i.e. 4 times an hour) of a catchment. The rainfall
>should be plotted in bars, the flow as a line. Both on the same X
>axe (time) but with different Y axes.
>
>The problem is the plot() function does not support add=TRUE...
>
>Furthermore I'm not sure what's the best format for input data.
>Now it are two data frames with a time (POSIXct) and a
>rainfall/flow part. Maybe time series are easier, but in
>
>ts(data = NA, start = X,...
>
>X should be a number or a vector. how does this coresponds to a
>data and hour (e.g. april 26,2002, 15:00:00)?
>
>Thanks,
>
>Institute for Land and Water Management
>Katholieke Universiteit Leuven
>
>---------------------------------------------------------------
>
>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
>r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
>Send "info", "help", or "[un]subscribe"
>(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```