2
我想使的箱線圖用兩種不同的因素:如何在R中重新定義晶須的兩級盒式空間?
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軸上相同位置的兩個不同boxplot被繪製在彼此的頂部,而不是像調用geom_boxplot()時那樣彼此相鄰。有一個更好的方法嗎?