简体   繁体   中英

Label whiskers on ggplot boxplot when there are outliers

I want to label the ends of the whiskers in ggplot's boxplots, not the minimum and maximum values, which in my data are often outliers.

I've tried using the code found here: annotate boxplot in ggplot2 , but as you can see from the output in factor(cyl)=8 (the blue), the absolute minimum and maximum values are labeled, not the points where the whiskers end.

This is the output: 在此处输入图片说明

ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(cyl))) + 
  geom_boxplot(width=0.6) +
  stat_summary(geom="text", fun.y=quantile,
           aes(label=sprintf("%1.1f", ..y..), color=factor(cyl)),
           position=position_nudge(x=0.33), size=3.5) +
theme_bw()

In the example given, I want the whiskers on factor(cyl) labeled, not the outliers.

Thanks for any help you all can provide.

Boxplots use boxplots.stats . You can directly use this in your stat_summary :

ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(cyl))) + 
  geom_boxplot(width=0.6) +
  stat_summary(
    aes(label=sprintf("%1.1f", ..y..), color=factor(cyl)),
    geom="text", 
    fun.y = function(y) boxplot.stats(y)$stats,
    position=position_nudge(x=0.33), 
    size=3.5) +
  theme_bw()

在此处输入图片说明

If you only need the whiskers, simply use boxplot.stats(y)$stats[c(1, 5)] instead.

welcome

Kind of works tm, I don't get why 8 cylinders breaks

library(tidyverse)

outlier_range <- function(x) {
  first_quantile  <-  quantile(x,0.25)
  third_quantile <-  quantile(x,0.75)
  iqr <- IQR(x)
  outlier_lower <- max(min(x), first_quantile  - 1.5 * iqr)
  outlier_higher <- min(max(x), third_quantile + 1.5 * iqr) 

  return(c(outlier_lower, outlier_higher))
}


ggplot(mtcars) +
  aes(x=factor(cyl), y=mpg, fill=factor(cyl)) + 
  geom_boxplot(width=0.6) +
  theme_bw() +
  stat_summary(geom="text", fun.y=outlier_range,
               aes(label=sprintf("%1.1f", ..y..), color=factor(cyl)),
               position=position_nudge(x=0.33), size=3.5)

Pigbacking on @Axeman:

ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(cyl))) + 
  geom_boxplot(width=0.6) +
  stat_summary(
    aes(label=sprintf("%1.1f", ..y..), color=factor(cyl)),
    geom="text", 
    fun.y = function(y) boxplot.stats(y)$stats[c(1,5)],
    position=position_nudge(x=0.33), 
    size=3.5) +
  theme_bw()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM