4

我有一个与此类似的数据,然后使用格子生成箱线图:

mydata <- data.frame(Y = rnorm(3*1000),
                  INDFACT =rep(c("A", "B", "C"), each=1000),
                  CLUSFACT=factor(rep(c("M","F"), 1500)))
library(lattice)
bwplot(Y ~ INDFACT | CLUSFACT, data=mydata, layout=c(2,1))

我的问题是我想为每个因素 A、B 和 C 设置不同的颜色。我试过这个:

bwplot(Y ~ INDFACT | CLUSFACT, data=mydata, layout=c(2,1), col=c("red","blue","green"))

但它只是改变了点的颜色。我想要的是改变整个颜色(点,盒子和伞)。

有没有办法做到这一点?

4

3 回答 3

8
names(trellis.par.get())
 [1] "grid.pars"         "fontsize"          "background"        "panel.background"  "clip"             
 [6] "add.line"          "add.text"          "plot.polygon"      "box.dot"           "box.rectangle"    
[11] "box.umbrella"      "dot.line"          "dot.symbol"        "plot.line"         "plot.symbol"      
[16] "reference.line"    "strip.background"  "strip.shingle"     "strip.border"      "superpose.line"   
[21] "superpose.symbol"  "superpose.polygon" "regions"           "shade.colors"      "axis.line"        
[26] "axis.text"         "axis.components"   "layout.heights"    "layout.widths"     "box.3d"           
[31] "par.xlab.text"     "par.ylab.text"     "par.zlab.text"     "par.main.text"     "par.sub.text"   

所以你想改变伞和点和盒子,但没有说是不是矩形的填充。我猜它是矩形线,因为同时填充和点是没有意义的。

bwp <- bwplot(Y ~ INDFACT | CLUSFACT, data=mydata, layout=c(2,1), 
               par.settings = list( box.umbrella=list(col= c("red", "green", "blue")), 
                                    box.dot=list(col= c("red", "green", "blue")), 
                                    box.rectangle = list(col= c("red", "green", "blue")) 
              )                    )
bwp

在此处输入图像描述

于 2012-04-18T23:22:05.037 回答
3

尝试设置box.rectangle参数:

bwplot(Y ~ INDFACT | CLUSFACT, data=mydata, layout=c(2,1),
        par.settings = list(box.rectangle = list(fill= rep(c('red','blue','green'),2)))

在此处输入图像描述

同样,我认为有些box.dot参数box.umbrella可以满足您的期望。

于 2012-04-18T23:06:08.907 回答
1

只是因为它很有趣,这样做ggplot2看起来像:

library(ggplot2)
ggplot(mydata, aes(x=INDFACT, y=Y, fill=INDFACT))+ 
  geom_boxplot()+
  facet_wrap(~CLUSFACT)
于 2012-04-18T23:20:55.063 回答