Skip to content

Instantly share code, notes, and snippets.

@nathancday
Last active April 18, 2018 13:26
Show Gist options
  • Save nathancday/3137e120cc901b0740f9c0a360a7e5cc to your computer and use it in GitHub Desktop.
Save nathancday/3137e120cc901b0740f9c0a360a7e5cc to your computer and use it in GitHub Desktop.
Help_for_ShStudent.R
# https://stackoverflow.com/questions/49880268/ggplot-how-to-remove-the-outliers-from-multiple-boxplots?noredirect=1#49880268
library(tidyverse)
data(iris)
iris_long <- gather(iris, "key", "val", -Species)
# with the outliers
ggplot(iris_long, aes(Species, val)) +
geom_boxplot() +
facet_wrap(~key, scales = "free")
# build our own without the outliers using the underlying function boxplot.stats()
new_iris <- group_by(iris_long, key, Species) %>%
nest() %>%
mutate(stats = map(data, ~ unlist(.) %>%
boxplot.stats(x = ., do.out = FALSE) %>%
.$stats %>%
bind_rows %>%
set_names(c("ymin", "lower", "middle", "upper", "ymax"))))
stats <- unnest(new_iris, stats)
# that's better :)
ggplot(stats) +
geom_boxplot(aes(x = Species, middle = middle, lower = lower, upper = upper, ymin = ymin, ymax = ymax),
stat = "identity") +
facet_wrap(~key, scales = "free")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment