Control legend colors in tactile::bwplot2

140 Views Asked by At

In this reproducible example below with the tactile package, the colors are automatically chosen for the boxplots and corresponding legend. However, I would like to customize the colors of the boxplots and legend.

tactile::bwplot2(runif(1000) ~ cut(runif(1000), c(0,0.3,0.6,1)) | as.factor(c(1,2,3)), 
             groups = sample(1:2, 1000, replace = TRUE), auto.key = TRUE)

boxplots with default colors

However, when I tried to do this, the colors in the boxplots changed but the legend colors did not:

Here I create a new color scheme:

coolNewPars <- list(superpose.symbol = list(pch = 21, cex = 2, col = "gray20",
                           fill = continentColors$color))

And then plot the boxplots again, with auto.key instructed to place the legend contents into 2 columns and the par.settings set to coolNewPars:

tactile::bwplot2(runif(1000) ~ cut(runif(1000), c(0,0.3,0.6,1)) | as.factor(c(1,2,3)), 
             groups = sample(1:2, 1000, replace = TRUE), auto.key = list(columns = 2),par.settings = coolNewPars)

boxplots with coolNewPars colors

How do I force the legend colors to match the coolNewPars colors?

1

There are 1 best solutions below

0
Johan Larsson On BEST ANSWER

The problem is that lattice::panel.superpose() uses trellis.get.par("superpose.symbol") to differentiate between groups, whilst the function that draws the key uses "superpose.polygon", or something like it.

In any case, here is a solution (although it is awkward):

coolNewPars <- list(superpose.polygon = list(col = 2:3),
                    superpose.symbol = list(fill = 2:3))

tactile::bwplot2(runif(1000) ~ cut(runif(1000), c(0,0.3,0.6,1)) | as.factor(c(1,2,3)), 
                 groups = sample(1:2, 1000, replace = TRUE), 
                 auto.key = TRUE,
                 par.settings = coolNewPars)

enter image description here