Remove whiskers in box-whisker-plot

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")

enter image description here


We only need to add the argument 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

enter image description here

Tags:

R

Ggplot2

Boxplot