[R] Second y-axis in xyplot (lattice) where y1 and y2 have different ranges
Andy Bunn
Andy.Bunn at wwu.edu
Mon Jun 18 22:34:33 CEST 2007
Hi all,
I realize this is asking a lot of lattice, but I want to add a second y
axis inside a xyplot and have y1 and y2 have different ranges. Given dat
below, I can add a second y axis by overlaying a new plot with
par(new=T) and label axis 4 with standard graphics. I've seen an example
for doing something similar in xyplot even though Deepayan has indicated
that lattice isn't the right tool for the job.
However, is there a way to gracefully add a second y-axis to a xyplot
where y1 and y2 have different scales as in the example below? I've seen
the experimental tools to focus and modify lattice graphics but do not
know if these are applicable.
I have unreasonable faith that lattice can do anything. Since my
eventual goal is to make use of a grouping variable as with dat2 below,
lattice will be preferable to complex layouts. Thanks, Andy
dat <- data.frame(Year = 1751:2000,
Stuff = rnorm(250),
Samples = floor(seq(5,30,length.out=250)
+rnorm(250,5)),
Grp = rep('SiteOne',250))
par(mar=c(5,4,4,4) + 0.1)
plot(Stuff~Year, data=dat, type='l')
par(new=T)
plot(Samples~Year, data=dat, type="l", axes=F, bty="n",
xlab="", ylab="")
axis(4, at=pretty(range(dat$Samples)))
mtext("Number of Samples", 4, 3)
xyplot(Stuff + Samples ~ Year | Grp, data=dat,
layout = c(1, 1),
panel = panel.superpose.2,
ylab = "Stuff",
legend = list(right =
list(fun = grid::textGrob("Samples", rot = 90))),
type = c('l', 'l'))
dat2 <- data.frame(Year = rep(1751:2000,2),
Stuff = rep(rnorm(250),2),
Samples = rep(floor(seq(5,30,length.out=250)+
rnorm(250,5)),2),
Grp = c(rep('SiteOne',250),
rep('SiteTwo',250)))
xyplot(Stuff + Samples ~ Year | Grp, data=dat2,
panel = panel.superpose.2,
ylab = "Stuff",
legend = list(right =
list(fun = grid::textGrob("Samples", rot = 90))),
type = c('l', 'l'))
More information about the R-help
mailing list