📅  最后修改于: 2022-03-11 15:00:33.398000             🧑  作者: Mango
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)