[R] second y-axis in lattice xyplot

Kara Przeczek przeczek at unbc.ca
Tue Aug 18 23:50:22 CEST 2009


Dear R-help,
I recently discovered lattice xyplot, but have been struggling with plotting a second y-axis. I am able to do this with plot() but can't figure it out in xyplot. Any help would be appreciated.
I have found help on adding a second Y-axis when the data is all part of the same data frame, but I have two different data frames.
Both data sets are being plotted against "Date". However the first data set only plots a few points on select dates whereas the second data set is plotted daily. I would like to plot them both with the same x-axis, but separate (labeled) y-axes. Is this possibly in lattice xyplot without too much hassle? Or am I better off plotting with plot()?

#The first data set plot:
trellis.par.set(superpose.symbol = list(cex = 1.3, pch = c(15,0,17,2), col=c("blue","blue","red","red")) )

xyplot(melt ~ Date, data = df1, groups = si, col = c("blue","blue","red","red"),
       pch= c(15,0,17,2), type = 'p',
       auto.key = list(points = TRUE, lines = FALSE, x=0.8, y=0.15, corner = c(0,1))
          )

#The second data set plot:
xyplot(AT ~ Date, data = df2,
          type = 'o', lty = 1, col = "black"
           )

Example data:
#df1:
Date <- as.Date(rep(c("2008-04-27","2008-05-02"),each=4), format="%Y-%m-%d")
si <- factor(rep(c("SO","SF","NO","NF"),2))
melt <- c(4.7,7.4,17.8,30.8,2.2,11.7,10.1,17.3)
df1 <- data.frame(Date, si, melt)

#df2:
Date <- as.Date(c("2008-04-27","2008-04-28","2008-04-29","2008-04-30","2008-05-01","2008-05-02"), format="%Y-%m-%d")
AT <- c(7.4,4.5,3.2,2.1,2.0,1.1)
df2 <- data.frame(Date,AT)

Thank you very much for your time.
Kara




More information about the R-help mailing list