[R] bwplot with pch = "|"

Deepayan Sarkar
Tue, 02 Mar 2010 11:08:09 -0000

On Mon, Mar 1, 2010 at 10:49 AM, Duncan Mackay <mackay at northnet.com.au>
wrote:
> Dear All
>
> Below is a toy example of a modified standard bwplot.
>
> require(lattice)
> DF <-
> data.frame(site = rep(1:5, each = 20),
> ? ? ? ? ? height = rnorm(100))
>
> bwplot(site ~ height,DF,
> pch = "|",
> par.settings = list(strip.background = list(col = "transparent"),
> ?box.rectangle = list(col = "grey70",lty = 1),
> ?box.umbrella = list(col = "grey70",lty = 1),
> ?plot.symbol = list(alpha = 1,col = "grey70",cex = 1,pch = 20),
> ?superpose.symbol = list(cex = rep(0.7, 7),col = "black", pch =
rep(20,7)))
> )
>
> The help guide shows that pch = "|" is a special case.
> This give me a line across the box which is what I want but how do I make
it
> thicker and red.

The part of panel.bwplot() responsible for this is

        if (all(pch == "|"))
        {
            mult <- if (notch) 1 - notch.frac else 1
            panel.segments(blist.stats[, 3],
                           levels.fos - mult * blist.height / 2,
                           blist.stats[, 3],
                           levels.fos + mult * blist.height / 2,
                           lwd = box.rectangle$lwd,
                           lty = box.rectangle$lty,
                           col = box.rectangle$col,
                           alpha = alpha)
        }

which shows that you are stuck with the same color as the rest of the
box. However, you can add your own thick red lines in a custom panel
function:

bwplot(site ~ height,DF,
       pch = "|",
       panel = function(x, y, ...) {
           panel.bwplot(x, y, ...)
           meds <- tapply(x, y, median)
           ylocs <- seq_along(meds)
           panel.segments(meds, ylocs - 1/4,
                          meds, ylocs + 1/4,
                          lwd = 2, col = "red")
       })

-Deepayan