2

I can do this without problems:

boxplot(coef ~ habitat, data = res)
abline(h = 0, col = "green")

But when I use lattice, the horizontal line is misplaced:

bwplot(coef ~ habitat, data = res)
abline(h = 0, col = "green")

enter image description here

I tried to use panel.abline instead but that places the green line on top of the picture.

Tomas
  • 57,621
  • 49
  • 238
  • 373

1 Answers1

5

Use a panel function; order the contained functions in the order you'd like parts added; make use of ... to avoid having to know / manage all parameters to the panel function

bwplot(coef ~ habitat, data = res, panel=function(...) {
    panel.abline(h=0, col="green")
    panel.bwplot(...)
})

The notion of a panel function makes more sense when there are several panels, e.g.,

res = data.frame(coef=rnorm(99) + (-1):1, habitat=sample(letters[1:3], 99, TRUE),
                 grp=c("W", "X", "Y"))
bwplot(coef ~ habitat | grp, data = res, panel=function(x, y, ...) {
    ## green line at panel-median y value
    panel.abline(h=median(y), col="green")
    panel.bwplot(x, y, ...)
})
Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
  • Thanks Martin, works as a charm! This is very strange syntax, I am not sure if I will be into lattice :-) – Tomas Aug 16 '13 at 14:10