有没有一种直接的方法可以从 in 中的箱须图中移除ggplot2胡须R?我想只保留盒子本身。
MWE:
library("ggplot2")
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(outlier.size = 0)
我们只需要添加参数coef = 0:
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(outlier.shape = NA, coef = 0) # Or outlier.size = -1 instead of outlier.shape = NA

一种方法是用于stat_summary_df()计算中位数、25 和 75 个百分位数,然后用geom="crossbar". 可以在"median_hilow"内部自动完成stat_summary_df()。为此,您需要在绘图之前添加库Hmisc并定义函数)。stat_summary_df(默认值为"median_hilow"2.5 和 97.5 个百分位数,因此您需要添加参数conf.int=0.5。
stat_sum_df <- function(fun, geom="crossbar", ...) {
stat_summary(fun.data=fun, colour="red", geom=geom, width=0.2, ...)
}
library(Hmisc)
ggplot(mtcars, aes(factor(cyl), mpg)) +
stat_sum_df("median_hilow",conf.int=0.5,fill="white")
