Remove whiskers in box-whisker-plot

馋奶兔 提交于 2019-11-29 10:14:42

We only need to add the argument coef = 0:

library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(outlier.size = 0, coef = 0)

One way would be to use stat_summary_df() to calculate meadian, 25 and 75 percentiles and then plot those data with geom="crossbar". Automatically it can be done with "median_hilow" inside the stat_summary_df(). For this you will need to add library Hmisc and also define function stat_summary_df() before plotting. Default values for "median_hilow" is 2.5 and 97.5 percentiles, so you need to add argument 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")

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!