Remove whiskers in box-whisker-plot

后端 未结 2 1094
借酒劲吻你
借酒劲吻你 2020-12-19 03:26

Is there a straightforward way to remove the whiskers from a box-whisker-plot in ggplot2 in R? I\'d like to keep only the boxes themselves.

相关标签:
2条回答
  • 2020-12-19 03:53

    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

    0 讨论(0)
  • 2020-12-19 04:03

    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

    0 讨论(0)
提交回复
热议问题