2 つの異なる要素を使用して箱ひげ図を作成したいと思います。
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 軸の同じ位置にある 2 つの異なる boxplot が、geom_boxplot() が呼び出されたときのように隣同士ではなく、重ねて描画されます。これを行うより良い方法はありますか?