How to label ggplot2 boxplot outliers with a third variable?

Nip :
library(ggplot2)

A <- c(rep(LETTERS[1:5],2))
B <- rep(c("one", "two"),5)
set.seed(200)
C <- round(rnorm(10),2)
dff <- data.frame(A,B,C)
dff

ggplot(dff, aes(x=B, y=C, fill=B)) + 
    geom_boxplot()

Is it possible to use A to label the outliers?

Caitlin :

Here's a solution to label only the outliers in your data:

library(tidyverse)
outlier <- dff %>%
  group_by(B) %>%
  summarise(outlier = list(boxplot.stats(C)$out))


ggplot(dff, aes(x=B, y=C, fill=B)) + 
  geom_boxplot() +
  geom_text(aes(label = if_else(C %in% unlist(outlier$outlier), as.character(A), "")), position=position_nudge(x=-.1))                                              

which produces this plot:

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=342018&siteId=1