Hi, I want to add a dashed vertical line to a number of xyplots. Here is a simple script of the type of plot I have but then I want to add a reference line to 1995 on each of the panels. I have tried panel.abline and other suggestions on the forum but can't get it to work. plot<-rep(letters[1:3],each=10) year<-rep(1991:2000,times=3) matter<-rep(seq(2,6.5,0.5),times=3) xyplot(matter~year|plot,type="l") Cheers, Chris -- View this message in context: http://www.nabble.com/adding-a-reference-line-to-an-xyplot-tp22117545p22117545.html Sent from the R help mailing list archive at Nabble.com.
Hi, try this: p <- xyplot(matter~year|plot,type="l") update(p, panel=function(...){ panel.xyplot(...) panel.abline(v=1995) } ) On 20 Feb 2009, at 09:34, Chris Bennett wrote:> > > Hi, > > I want to add a dashed vertical line to a number of xyplots. > > Here is a simple script of the type of plot I have but then I want > to add a > reference line to 1995 on each of the panels. I have tried > panel.abline and > other suggestions on the forum but can't get it to work. > > plot<-rep(letters[1:3],each=10) > year<-rep(1991:2000,times=3) > matter<-rep(seq(2,6.5,0.5),times=3) > xyplot(matter~year|plot,type="l") > > Cheers, > Chris > -- > View this message in context: http://www.nabble.com/adding-a-reference-line-to-an-xyplot-tp22117545p22117545.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
Another approach using latticeExtra, more ggplot2-like: p <- xyplot(matter~year|plot,type="l") p + latticeExtra::layer(panel.abline(v=1995)) On 20 Feb 2009, at 09:34, Chris Bennett wrote:> > > Hi, > > I want to add a dashed vertical line to a number of xyplots. > > Here is a simple script of the type of plot I have but then I want > to add a > reference line to 1995 on each of the panels. I have tried > panel.abline and > other suggestions on the forum but can't get it to work. > > plot<-rep(letters[1:3],each=10) > year<-rep(1991:2000,times=3) > matter<-rep(seq(2,6.5,0.5),times=3) > xyplot(matter~year|plot,type="l") > > Cheers, > Chris > -- > View this message in context: http://www.nabble.com/adding-a-reference-line-to-an-xyplot-tp22117545p22117545.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
On Fri, Feb 20, 2009 at 01:34:02AM -0800, Chris Bennett wrote:> I want to add a dashed vertical line to a number of xyplots. > > Here is a simple script of the type of plot I have but then I want to add a > reference line to 1995 on each of the panels. I have tried panel.abline and > other suggestions on the forum but can't get it to work. > > plot<-rep(letters[1:3],each=10) > year<-rep(1991:2000,times=3) > matter<-rep(seq(2,6.5,0.5),times=3) > xyplot(matter~year|plot,type="l")The panel function is the way to go: xyplot( matter~year|plot, type="l", panel=function(...){ panel.xyplot(...) panel.abline(v=1995) } ) BTW: it is not a good idea to use the name of builting functions as a variable name (plot). cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany http://mips.gsf.de/staff/pagel
Hi r-help-bounces at r-project.org napsal dne 20.02.2009 10:34:02:> > > Hi, > > I want to add a dashed vertical line to a number of xyplots. > > Here is a simple script of the type of plot I have but then I want toadd a> reference line to 1995 on each of the panels. I have tried panel.ablineand> other suggestions on the forum but can't get it to work. > > plot<-rep(letters[1:3],each=10) > year<-rep(1991:2000,times=3) > matter<-rep(seq(2,6.5,0.5),times=3) > xyplot(matter~year|plot,type="l")Here is a function which I adapted from Gabor Grothendieck's suggestion addLine<- function(a=NULL, b=NULL, v = NULL, h = NULL, ..., once=F) { tcL <- trellis.currentLayout() k<-0 for(i in 1:nrow(tcL)) for(j in 1:ncol(tcL)) if (tcL[i,j] > 0) { k<-k+1 trellis.focus("panel", j, i, highlight = FALSE) if (once) panel.abline(a=a[k], b=b[k], v=v[k], h=h[k], ...) else panel.abline(a=a, b=b, v=v, h=h, ...) trellis.unfocus() } } It works on plotted xyplot, accepts single value or vector of values which can be different for each panel. Regards Petr> > Cheers, > Chris > -- > View this message in context:http://www.nabble.com/adding-a-reference-line-> to-an-xyplot-tp22117545p22117545.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.