[R] second y-axis in lattice xyplot

Duncan Mackay mackay at northnet.com.au
Wed Aug 19 00:22:14 CEST 2009


At 07:50 19/08/2009, you wrote:
>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
>
>______________________________________________
>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.

Hi Kara

just a quick fix. you will have to look at ?doubleYScale to improve things

  require(latticeExtra)
  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))
+           )
+
+ ,xyplot(AT ~ Date, data = df2,
+           type = 'o', lty = 1, col = "black"
+            )
)

HTH

Duncan Mackay
Department of Agronomy and Soil Science
Univeristy of New England
ARMIDALE NSW 2351
Email: home : mackay at northnet.com.au




More information about the R-help mailing list