Reputation: 443
I've already been able to print outlier values.
ggplot(mtcars) +
aes(x = "", y = wt) +
geom_boxplot() +
geom_text(data=. %>%
mutate(type=rownames(.)) %>%
filter(wt %in% boxplot.stats(wt, coef=1.4)$out),
aes(label=type,y=wt),
nudge_x=0.01, colour="red", size=3, hjust=0) +
theme_classic()
I know the ggplotly function shows this information, but it doesn't allow saving along with the image. @Sparhawk and @CuriousJ presents a similar solution using the boxplot function.
I tried this way but to no avail.
ggplot(mtcars) +
aes(x = "", y = wt) +
geom_boxplot() +
geom_text(data=. %>%
mutate(type=rownames(.)) %>%
filter(wt %in% boxplot.stats(wt, coef=1.4)$out),
aes(label=type,y=wt),
nudge_x=0.01,size=3, hjust=0) +
geom_text(data=mtcars, aes(
label=boxplot.stats(wt)$start,
y=boxplot.stats(wt)$start),
nudge_x=0.01, size=3, hjust=0) +
theme_classic()
any idea how to solve this problem?
Upvotes: 1
Views: 273
Reputation: 26650
There are no points for the q1 / median / q3 (these are summary statistics, not individual points), but if you want to include the corresponding values on the plot (min, q1, median, q3, max), you could use:
library(tidyverse)
boxplot_labels <- tibble(y = boxplot.stats(mtcars$wt, coef=1.4)$stats,
x = c(1.1, 1.5, 1.5, 1.5, 1.1)) %>%
mutate(labels = paste("\u2190 y =", y))
ggplot(mtcars) +
aes(x = "", y = wt) +
geom_boxplot() +
geom_text(data=. %>%
mutate(type=rownames(.)) %>%
filter(wt %in% boxplot.stats(wt, coef=1.4)$out),
aes(label=type,y=wt),
nudge_x=0.01, colour="red", size=3, hjust=0) +
geom_text(data = boxplot_labels,
aes(x = x, y = y, label = labels), color = "red") +
theme_classic()
Created on 2021-07-20 by the reprex package (v2.0.0)
Upvotes: 3