2013-11-24 38 views
4

我有以下代碼,允許通過函數在lattice分組垂直方塊圖。重複的例子..與bwplot分組水平boxplot

data(mpg, package = "ggplot2") 

bwplot(hwy~class, data = mpg, groups = year, 
    pch = "|", box.width = 1/3, 
    auto.key = list(points = FALSE, rectangles = TRUE, space = "right"), 
    panel = panel.superpose, 
    panel.groups = function(x, y, ..., group.number) { 
     panel.bwplot(x + (group.number-1.5)/3, y, ...) 
    }) 

這工作得很好,但我想箱圖是水平的,所以我改變了第一線,保持一切等於:

bwplot(class~hwy, data = mpg, groups = year, ... 

但圖出來像這Output。我試圖玩弄代碼而沒有成功。我有兩個問題:首先,我怎麼能夠或者可以讓這些箱子互相疊加?第二,也是更普遍的是,我怎樣才能將彩色面板設置爲灰度,所以情節出現灰色或黑白兩色?

回答

3

如果不是關鍵性的,你使用bwplot,您可以嘗試ggplot

ggplot(data = mpg, aes(x = class, y = hwy, fill = factor(year))) + 
    geom_boxplot() + 
    coord_flip() + 
    scale_fill_grey(start = 0.5, end = 0.8) + 
    theme_classic() 

enter image description here

6
  • 你也應該顛倒X和Y角色panel.groups功能。
  • 使用trellis.par.set改變疊加符號

enter image description here

data(mpg, package = "ggplot2") 
library(latticeExtra) 
mycolors <- grey.colors(5, start = 0.1, end = 0.9) 
trellis.par.set(superpose.symbol = list(fill = mycolors,col=mycolors)) 
bwplot(class~hwy, data = mpg, groups = year, 
       pch = "|", box.width = 1/3, 
       panel = panel.superpose, 
       panel.groups = function(x, y,..., group.number) 
        panel.bwplot(x,y + (group.number-1.5)/3,...) 
      ) 

的填充顏色,但它可能是更容易在這裏使用GGPLOT2解決方案。

+1

謝謝,'y +(group.numbers-1.5)/ 3)'技巧是一個漂亮! –