使用与 Marco Sandri 的答案相同的数据集,两种可能的黑客攻击可供考虑。
黑客 1。如果你真的不需要它来绘图,只需静态 ggplot 图像:
ggplot(dat, aes(x=cond, y=rating, fill=cond)) +
geom_boxplot() +
geom_boxplot(aes(color = cond),
fatten = NULL, fill = NA, coef = 0, outlier.alpha = 0,
show.legend = F)
这覆盖了原始箱线图,其版本基本上是外箱的轮廓,隐藏了中位数 (fatten = NULL)、填充颜色 (fill = NA)、胡须 (coef = 0) 和异常值 (outlier.alpha = 0) .
但是,它似乎不适用于 plotly。我已经用 ggplot2 的开发版本(由 plotly 推荐)对其进行了测试,但无济于事。请参阅下面的输出:
黑客 2。如果您需要它在情节中工作:
ggplot(dat %>%
group_by(cond) %>%
mutate(rating.IQR = case_when(rating <= quantile(rating, 0.3) ~ quantile(rating, 0.25),
TRUE ~ quantile(rating, 0.75))),
aes(x=cond, y=rating, fill=cond)) +
geom_boxplot() +
geom_boxplot(aes(color = cond, y = rating.IQR),
fatten = NULL, fill = NA)
(ggplot输出同上)
plotly 似乎无法理解 coef = 0 和 output.alpha = 0 命令,因此这个 hack 创建了 y 变量的修改版本,使得 P30 以下的所有内容都设置为 P25,而上面的所有内容都设置为 P75。这将创建一个没有异常值、没有胡须的箱线图,并且中位数与 P75 的箱形上限位于一起。
虽然比较麻烦,但是在情节上可以用: