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)
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)
How do I force the legend colors to match the coolNewPars colors?


The problem is that
lattice::panel.superpose()usestrellis.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):