Skip to content

adding a reference line to an xyplot

5 messages · chris20, Baptiste Auguie, Philipp Pagel +1 more

#
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
#
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:

            
_____________________________

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:

            
_____________________________

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:
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
#
Hi

r-help-bounces at r-project.org napsal dne 20.02.2009 10:34:02:
add a
and
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
http://www.nabble.com/adding-a-reference-line-
http://www.R-project.org/posting-guide.html