在触觉中控制图例颜色:: bwplot2

时间:2018-10-08 01:40:09

标签: lattice bwplot

在下面带有触觉包的可复制示例中,将自动为箱线图和相应的图例选择颜色。但是,我想自定义箱形图和图例的颜色。

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

但是,当我尝试执行此操作时,方框图中的颜色发生了变化,但图例颜色却没有:

在这里我创建一个新的配色方案:

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

然后再次绘制箱线图,并指示auto.key将图例内容放入两列,并将参数设置设置为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

如何强制图例颜色与coolNewPars颜色匹配?

1 个答案:

答案 0 :(得分:1)

问题在于lattice::panel.superpose()使用trellis.get.par("superpose.symbol")来区分组,而 绘制键的函数使用"superpose.polygon"或类似的名称。

无论如何,这是一个解决方案(尽管很尴尬):

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