Skip to content

Instantly share code, notes, and snippets.

@rabutler
Created May 5, 2017 21:14
Show Gist options
  • Save rabutler/f6cc887a1dc37a35e2032d6f1b451d32 to your computer and use it in GitHub Desktop.
Save rabutler/f6cc887a1dc37a35e2032d6f1b451d32 to your computer and use it in GitHub Desktop.
Returns a generic boxplot legend, using the stat-boxplot-custom code
library(devtools)
library(ggplot2)
source_gist("38e70de658b48e166f90", filename = "theme-blank.R")
source_gist("bd97a6f49db87860f987156842fd4ee5", filename = "stat-boxplot-custom.R")
getBoxplotLegend <- function(qs = c(.05, .25, .5, .75, .95)) {
df <- data.frame(x = "a", y = c(-3:1,5:95,99:103))
dfQs <- quantile(df$y, probs = qs)
os <- c(mean(c(max(df$y), dfQs[5])), mean(c(min(df$y), dfQs[1])))
gg <- ggplot(df, aes(x, y)) +
stat_boxplot_custom(width = .9, qs = qs) +
theme_blank() +
annotate("text", x = 1.01, y = dfQs[c(1,5)], label = c("5th percentile", "95th percentile"), hjust = 0) +
annotate("text", x = 1, y = dfQs[3], label = "Median", vjust = 1) +
annotate("text", x = 1.01, y = dfQs[c(2,4)], label = c("25th percentile", "75th percentile"), hjust = 0, vjust = 1) +
annotate("text", x = 1.01, y = os, label = "Outliers", hjust = 0)
gg
}
@rabutler
Copy link
Author

rabutler commented May 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment