[R] panel.abline and xyplot

Dieter Menne dieter.menne at menne-biomed.de
Thu Jun 8 12:52:56 CEST 2006


Marc Bernard <bernarduse1 <at> yahoo.fr> writes:

> 
>   I am wondering on how to use the abline.xyplot with xyplot such that I will
have different vertical lines for
> each panel. More sepcifically, suppose that the xyplot generates 4 panels
defined by the combination of
> two binary variables: X_1 and X_2. i.e. 
> 
>   xyplot(Y ~ Z | X_1*X_2, data = df) 
>   abline(v = 5)  if X_1=0 and X_2 = 0 
>   abline(v = 10)  if X_1=0 and X_2 = 1
>   abline(v = 15) if X_1=1 and X_2 = 0
>   abline(v = 31) if X_1=1 and X_2 = 1
> 
>   when I used : xyplot(Y ~ Z | X_1*X_2, data=df, panel = function(x,y)
{panel.xyplot(x,y)
> panel.abline(v=70)}) it produces the same vertical line in the four panels.
> 

I use subscripts to display separately stored array data, but I am sure there
must be a more elegant of knowing what the current group is than by accessing
its first element.

Dieter


library(lattice)
quakes$Depth = as.factor(floor(quakes$depth/100))
quakes$Mag = as.factor(floor(quakes$mag))

whereline = expand.grid(
  Mag=levels(quakes$Mag),
  Depth=levels(quakes$Depth))
whereline$line = rnorm(nrow(whereline),-30,10)

xyplot(lat ~ long | Depth*Mag, data = quakes,
  panel = function(x,y,subscripts,...) {   
    mg = quakes[subscripts[1],] 
    with (whereline, 
      panel.abline(h= whereline[Mag==mg$Mag & Depth==mg$Depth,"line"]))
    panel.xyplot(x,y,...)
  }
  )



More information about the R-help mailing list