我想用两个不同的因素制作一个箱线图:
df <- data.frame(f1=factor(rbinom(100, 1, 0.45), label=c("m","w")),
f2=factor(rbinom(100, 1, 0.45), label=c("young","old")),
boxthis=rnorm(100))
ggplot(aes(y = boxthis, x = f2, fill = f1), data = df) + geom_boxplot()
我还想根据分位数重新定义胡须:
f <- function(x) {
r <- quantile(x, probs = c(0.025, 0.25, 0.5, 0.75, 0.975))
names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
r
}
ggplot(aes(y = boxthis, x = f2, fill = f1), data = df) + stat_summary(fun.data = f, geom="boxplot")
但是,当使用 stat_summary() 时,x 轴上同一位置的两个不同的箱线图彼此重叠绘制,而不是像调用 geom_boxplot() 时那样彼此相邻。有一个更好的方法吗?