[R] lattice question: how to change the dot on boxplot to line

Duncan Mackay mackay at northnet.com.au
Tue Dec 11 05:09:15 CET 2012


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:
>Hi,
>
>How does one change the dot for the median in a boxplot drawn using
>lattice? I have been looking at
>
> > names(trellis.par.get())
>  [1] "grid.pars"         "fontsize"          "background"
>  [4] "panel.background"  "clip"              "add.line"
>  [7] "add.text"          "plot.polygon"      "box.dot"
>[10] "box.rectangle"     "box.umbrella"      "dot.line"
>[13] "dot.symbol"        "plot.line"         "plot.symbol"
>[16] "reference.line"    "strip.background"  "strip.shingle"
>[19] "strip.border"      "superpose.line"    "superpose.symbol"
>[22] "superpose.polygon" "regions"           "shade.colors"
>[25] "axis.line"         "axis.text"         "axis.components"
>[28] "layout.heights"    "layout.widths"     "box.3d"
>[31] "par.xlab.text"     "par.ylab.text"     "par.zlab.text"
>[34] "par.main.text"     "par.sub.text"
>
>I tried playing around with dot.line and dot.symbol but could not
>figure out what to do. Is there anything else I could try.
>
>
>
>As a reproducible example, we can consider:
>
>library(lattice)
>bwplot(voice.part ~ height, data=singer, xlab="Height (inches)")
>
>I want the dots for the medians to be replaced by a line through the
>median.
>
>Any suggestions?
>
>Many thanks,
>Ranjan
>
>PS; Also, how do I get the rectangles to be filled? I tried the
>following:
>
>box.rectangle <- trellis.par.get("box.rectangle")
>box.rectangle$fill<-"opaque"
>
>(also tried "filled", "shaded", etc. to no avail)
>
>trellis.par.set("box.rectangle",  box.rectangle)
>
>but get errors in plotting.
>
>Many thanks again and best wishes,
>Ranjan
>
>____________________________________________________________
>FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list