0

I have a graph of plant species frequency by year for several sites that I am plotting using xyplot in the lattice package. I've figured out how to get the scatterplot for each species-site combo. However, I want to add an abline representing each year in which a chemical treatment was done. Chemical treatments were added in different years at each site, and I'd like to add a vertical abline for each species-site graph in which a chemical treatment was performed at that site. Here is my xyplot code:

library(plyr)
sp.1 <- data.frame(site=rep('a', 10), year=seq(2001, 2010, 1), year.trt=c(NA, NA, NA, NA, 2005, NA, NA, 2008, NA, NA), pl.1=rnorm(10, 4, 1), pl.2=rnorm(10, 6, 2))
sp.2 <- data.frame(site=rep('b', 10), year=seq(2001, 2010, 1), year.trt=c(2001, NA, NA, NA, NA, 2006, NA, NA, NA, NA), pl.1=rnorm(10, 5, 2), pl.2=rnorm(10, 4, 1))
sp.3 <- data.frame(site=rep('c', 10), year=seq(2001, 2010, 1), year.trt=c(NA, NA, NA, 2004, NA, NA, NA, NA, 2009, NA), pl.1=rnorm(10, 8, 1), pl.2=rnorm(10, 3, 3))
data <- rbind.fill(sp.1, sp.2, sp.3)

xy.plot <- xyplot(pl.1 + pl.2 ~ year | site, data=data, outer=T, type='l',              
as.table=T, xlab=c('Year'), ylab=c('Spp. Frequency (%)'),
panel=function(x, y,...){
    panel.xyplot(x,y, type='l')
    panel.abline(v=data$year.trt)
})
print(xy.plot)

So, the important line of code in this block is the 'panel.abline(v=test$trt.year)'. Currently, this plots all years in my dataset in which a chemical treatment was done, however, I'd like it to show in each panel which year a treatment was done for that specific site.

Any insight would be greatly appreciated.

Thanks,
Paul

  • 2
    Please supply a reproducible example. However, if I understand it correctly, a solution to your problem is likely going to involve `panel.number()`, as demonstrated, for example, [here](http://stackoverflow.com/questions/12828027/conditional-panel-function-in-lattice-for-multiple-y-variables/16577667#16577667) or [here](http://stackoverflow.com/questions/9746597/changing-background-color-in-xyplot/9752056#9752056). – Josh O'Brien Jan 17 '14 at 02:04
  • What is the right answer? – IRTFM Jan 17 '14 at 03:26
  • Okay, I've partially solved the problem. I've changed the line of concern to `panel.abline(v=data$year.trt[data$site==sites[panel.number()]])` where `sites` is `sites <- unique(data$site)` . This provided the vertical lines nicely, but only in the top row of plots contains the lines. How can I extend them to the rest of the plots in the figure? – logicForPresident Jan 17 '14 at 04:41
  • Nevermind. I realized that with my sites object I only had enough values to be able to index the first layer of panel numbers. I simply doubled my sites object (e.g. `sites <- rep(unique(data$sites), 2)` and then that covered it. Thanks Josh O'Brien!!! – logicForPresident Jan 17 '14 at 04:52

0 Answers0