boxplot with arbitrary design with ggplot2

## Settings for RMarkdown http://yihui.name/knitr/options#chunk_options
opts_chunk$set(comment = "", warning = FALSE, message = FALSE, tidy = FALSE, 
    echo = TRUE, fig.width = 7, fig.height = 7)
options(width = 116, scipen = 10)

setwd("~/statistics/Rmedstats/rgraphics/")

Reference

Assign x, y variables

library(ggplot2)
p1 <- ggplot(aes(y = qsec, x = factor(gear)), data = mtcars)

Regular boxplot

p1 + geom_boxplot() + geom_point(alpha = 1/5)

plot of chunk unnamed-chunk-3

Whisker-less boxplot

box.only <- function(x) {
  r <- quantile(x, probs = c(0.25, 0.25, 0.5, 0.75, 0.75))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}

p1 + stat_summary(fun.data = box.only, geom = "boxplot") + geom_point(alpha = 1/5)

plot of chunk unnamed-chunk-4

Min-max boxplot

min.max <- function(x) {
  r <- quantile(x, probs = c(0.00, 0.25, 0.5, 0.75, 1.00))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}

p1 + stat_summary(fun.data = min.max, geom = "boxplot") + geom_point(alpha = 1/5)

plot of chunk unnamed-chunk-5

Min-(-SD)-Mean-(+SD)-Max boxplot

min.mean.sd.max <- function(x) {
  r <- c(min(x), mean(x) - sd(x), mean(x), mean(x) + sd(x), max(x))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}

p1 + stat_summary(fun.data = min.mean.sd.max, geom = "boxplot") + geom_point(alpha = 1/5)

plot of chunk unnamed-chunk-6