[R] lattice: superposed boxplots with same colors forrectanglesandumbrellas and filled boxes

Gerrit Eichner Gerrit.Eichner at math.uni-giessen.de
Tue Dec 10 12:25:59 CET 2013


Thank you very much, Rich, for the fast and very helpful reply! It helped 
me to reach my goal.

  Regards -- Gerrit

PS: I extended your solution to a version that allows slightly finer 
control over the components of the boxplots, in particular if one wants to 
combine it with, e.g., panel.stripplot() or panel.average(). It was also 
possible to simplify my call of bwplot() a bit. The code is certainly not 
perfect and fully tested, but does work (for me ;-)), and follows as a 
little 'thank you':


panel.bwplot.constantColor <- function( ..., col, fill, cex, pch,
                                         dot.pch, umbrella.lty, box.alpha) {
  ## Date: Mon, 9 Dec 2013 17:52:38 -0500
  ## From: Richard M. Heiberger <rmh at temple.edu>
  ## Subject: Re: [R] lattice: superposed boxplots with same colors
  ##          for rectangles and umbrellas and filled boxes

  ## to be included in next version of the HH package
  box.save <- list( box.dot = trellis.par.get( "box.dot"),
                    box.rectangle = trellis.par.get( "box.rectangle"),
                    box.umbrella = trellis.par.get( "box.umbrella"),
                    plot.symbol = trellis.par.get( "plot.symbol"))
  trellis.par.set( box.dot = list( col = col),
                   box.rectangle = list( col = col, alpha = box.alpha),
                   box.umbrella = list( col = col, lty = umbrella.lty,
                                        alpha = box.alpha),
                   plot.symbol = list( col = col))
  panel.bwplot( ..., fill = col, cex = cex, pch = dot.pch)
  trellis.par.set( box.save)
  }


bwplot( Y ~ F1, groups = F2, data = Data, jitter.data = TRUE,
         col = c( "red", "blue"), box.alpha = 1/4,
         dot.pch = 17, umbrella.lty = 1, do.out = FALSE,
         panel = panel.superpose,
         panel.groups = panel.bwplot.constantColor)




> Thank you for the opportunity to illustrate this.  I solved this problem 
> last week and it will be included in the next version of the HH package 
> (about a month away).
>
> panel.bwplot.constantColor <- function(..., col, fill, cex, pch) {
>  ## to be included in next version of the HH package
>  box.save <- list(box.dot=trellis.par.get("box.dot"),
>                   box.rectangle=trellis.par.get("box.rectangle"),
>                   box.umbrella=trellis.par.get("box.umbrella"),
>                   plot.symbol=trellis.par.get("plot.symbol"))
>  trellis.par.set(box.dot=list(col=col),
>                  box.rectangle=list(col=col),
>                  box.umbrella=list(col=col),
>                  plot.symbol=list(col=col))
>  panel.bwplot(..., fill=col, cex=cex, pch=pch)
>  trellis.par.set(box.save)
> }
>
> bwplot( Y ~ F1, groups = F2, data = Data,
>        col = mycolors, fill = mycolors, jitter.data = TRUE, pch=19,
>        panel = panel.superpose,
>        panel.groups = function(..., pch, col, alpha){
>            panel.stripplot(...)
>            panel.bwplot.constantColor(..., pch=pch, col=col,
> alpha=alpha, do.out = FALSE)
>        },
>        par.settings = list( box.dot = list( alpha = myalpha),
>                             box.rectangle = list( col = mycolors,
>                                                   alpha = myalpha),
>                             box.umbrella = list( col = mycolors,
>                                                  alpha = myalpha))
>        )
>
>
> Rich
>
> On Mon, Dec 9, 2013 at 5:17 PM, Gerrit Eichner
> <Gerrit.Eichner at math.uni-giessen.de> wrote:
>> Dear R-list,
>>
>> I've been trying to produce a sort of an interaction plot wherein colored
>> stripplots and boxplots are superposed. In particular, I want the colors of
>> the (transparently) filled boxes to be the same as the colors of the box
>> borders (rectangle) and their whiskers (umbrella). Below I'm going to create
>> an artificial data set and provide the code with which I've come up so far,
>> and which is a fusion and adaptation of a few pieces of code I've found in
>> the help pages and the mail archives. It does almost what I want, but still
>> colors the rectangles and the umbrellas in black (of course, because it is
>> the setting in the example presented below). However, the latter is what I
>> want to change.
>>
>> x <- c( rep( 1:4, each = 10), rep( -2, 40))
>> Data <- data.frame( F1 = gl( 4, 10, length = 80, labels = LETTERS[ 1:4]),
>>                     F2 = gl( 2, 40), Y = x + rnorm( length( x)))
>>
>> mycolors <- c( "red", "blue")
>> myalpha <- 0.33
>> bwplot( Y ~ F1, groups = F2, data = Data,
>>         col = mycolors, fill = mycolors, jitter.data = TRUE,
>>         panel = panel.superpose,
>>         panel.groups = function(..., pch, col, alpha){
>>             panel.stripplot(...)
>>             panel.bwplot(..., do.out = FALSE)
>>         },
>>         par.settings = list( box.dot = list( alpha = myalpha),
>>                              box.rectangle = list( col = "black",
>>                                                    alpha = myalpha),
>>                              box.umbrella = list( col = "black",
>>                                                   alpha = myalpha))
>>         )
>>
>>
>> If I'd provide mycolors instead of "black" to the col-component of the
>> (sub-)lists given to par.settings, the coloring of the respective components
>> of the boxplots would not be what I want. Having studied the code of
>> panel.superpose() and panel.bwplot() it appears to me that panel.bwplot()
>> cannot access the settings created by panel.supperpose when it comes to
>> drawing the rectangle and umbrella of a boxplot. It only accesses box.[dot,
>> rectangle, umbrella], which does not give what I need.
>>
>> I may have overlooked the obvious and would be quite grateful if somebody
>> could give me a hint where to look further. Or is a workaround necessary
>> (and available)?
>>
>> Thanks a lot in advance!
>>
>>  Best Regards  --  Gerrit



More information about the R-help mailing list