[R] panel.abline {lattice} help

Erik Iverson eriki at ccbr.umn.edu
Thu Jun 10 04:11:28 CEST 2010


Xin,

Xin Ge wrote:
> Hi All,
> 
> I need a small help plotting median lines on lattice boxplots.
> 
> # Data
> a <- rep(c("A","B"), each=10)
> b <- rep(c("a","a+b","b","b+a"), each=5)
> c <- c(1,9,5,2,7,7,8,8,8,5,4,5,3,2,5,6,7,8,9,1)
> x <- data.frame(a, b, c)
> med.A <- median(subset(x$c, x$a=="A"))
> med.B <- median(subset(x$c, x$a=="B"))
> 
> median(group A) = 7
> median(group B) = 5
> 
> I need help to plot two (horizontal) lines, one for each subgroup.
> - Line 1 coordinates should be (0, 7) to (2.5, 7)
> - Line 2 coordinates should be (2.5, 5) to (5, 5)
> 
> I'm unable to control the length of these lines, can anyone please help me
> with this, I would highly appreciate any help. (I tried using panel.curve,
> but of no use)

There could be a better way, but here's a method using the lower-level grid 
functions.

bwplot(c ~ b, data = x,
    panel = function(...)
    {
       panel.abline(v=2.5, col.line="gray")
       grid.lines(x = unit(c(0, 2.5), "native"),
                  y = unit(c(med.B, med.B), "native"),
                  gp = gpar(col = "red"))
       grid.lines(x = unit(c(2.5, 5), "native"),
                  y = unit(c(med.A, med.A), "native"),
                  gp = gpar(col = "red"))
       panel.bwplot(...)
    }
)



More information about the R-help mailing list