Answers for "label outliers ggplot"

0

label outliers ggplot

is_outlier <- function(x) {
  return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}

z_mtcars %>%
pivot_longer(names_to = "variable", values_to = "value", -type) %>% 
group_by(variable) %>% 
mutate(outlier = if_else(is_outlier(value), type, NA_character_)) %>% 
ggplot(aes(x = variable, y = value, color = variable)) +
geom_boxplot() +
geom_text_repel(aes(label = outlier), na.rm = TRUE, show.legend = F)
Posted by: Guest on June-11-2021

Browse Popular Code Answers by Language