set.seed(3)
values <- rnorm(36)
library(ggplot2)
library(ggpattern)
mydat <- data.frame(time = factor(c(rep(0, 12), rep(1, 12), rep(2, 12))),
class = factor(rep(c(rep("A", 6), rep("B", 6)), 3)),
method = factor(rep(c(rep("Manual", 3), rep("Automatic", 3)), 6)),
value = values)
> head(mydat)
time class method value
1 0 A Manual -0.96193342
2 0 A Manual -0.29252572
3 0 A Manual 0.25878822
4 0 A Automatic -1.15213189
5 0 A Automatic 0.19578283
6 0 A Automatic 0.03012394
我有一个包含4个类别的数据集:2个类x 2个方法.我想在每个时间点用不同的 colored颜色 和图案做一个并排的盒子图.
> ggplot(mydat, aes(x = time, y = value, fill = method, pattern = class)) +
geom_boxplot_pattern(position = position_dodge(width = 0.8), aes(pattern = class, pattern_fill = class), pattern_density = 0.35) +
geom_boxplot(position = position_dodge(width = 0.8))
这为我提供了以下图案未正确对齐的情况:
我希望它看起来像这样: