我有以下代码,允许通过bwplot
中的lattice
函数分组垂直框图。可重复的例子..
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, ...
但图表就像这个一样。我试过玩代码没有成功。我有两个问题:首先,我怎么能,或者是否可以让箱图不相互叠加?其次,更一般地说,如何将彩色面板设置为灰度,因此情节会以灰色或黑白色调出现?
答案 0 :(得分:6)
panel.groups
函数中反转x和y角色。trellis.par.set
更改叠加符号的填充颜色
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 :(得分: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()