Skip to content
Prev 313368 / 398502 Next

lattice question: how to change the dot on boxplot to line

Hi

Try this (Deepayan gave me this in reply to a similar question 
several years ago)

  bwplot(voice.part ~ height, data=singer, xlab="Height (inches)",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")
            })

I have made the colour red as one of the lines overlays the box.

It gets a bit more involved with panel functions and horizontal  - try this

DF <-
data.frame(site = factor(rep(1:5, each = 20)),
            height = rnorm(100))

   bwplot(height~ site,DF,
          pch   = "|",
          panel = function(x, y, ..., horizontal) {

                     panel.bwplot(x, y, ...,  horizontal = horizontal)

                     if (horizontal) {

                       meds <- tapply(x, y,  median)
                       ylocs <- seq_along(meds)
                       panel.segments(meds, ylocs - 1/4,
                                      meds, ylocs + 1/4,
                                      lwd = 2, col = "red")

                     } else {

                       meds <- tapply(y, x, median)
                       xlocs <- seq_along(meds)
                       panel.segments(xlocs - 1/4, meds,
                                      xlocs + 1/4, meds,
                                      lwd = 2, col = "red")

                    } ## if (horizontal)
                  }  ## panel function
   ) ## bwplot

Regards

Duncan


Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
At 13:03 11/12/2012, you wrote: