Basic Boxplots with annotations in R

Boxplots in R can be a bit tricky (ugly actually), but here is an example below to help.  Below is the binomial distribution for p = 0.3, p = 0.5 and p = 0.7 with total number of trials n = 60 as a function of k successful trials.  Shown is the typical five statistics of 1st Quartile, median, mean, standard deviation and 3rd quartile.  The probability is on the horizontal axis.

 

# Setup window to show three graphs side by side par(mfrow = c(1, 3))
# Create a boxplot using rbinom(), displaying values for 1st Quartile,
# mean, median, standard deviation, 3rd Quartile. The mean is denoted
# using pch=8 (*) on the plot. Standard deviation is noted in the xlab.

bp1 <- boxplot(rbinom(60, 60, 0.3), col = "red", main = ("Binomial Distribution k=60 p=.3"), xlab = c(paste("p=0.3, mean", 60 * 0.3, sep = "="), paste("sd", sprintf("%0.2f", sqrt(60 * 0.3 * 0.7)), sep = "=")), ylab = "Value") points(x = 1, y = 60 * 0.3, pch = 8) text(1, 0.5 + 60 * 0.3, labels = 60 * 0.3) text(1.3, bp1$stats, labels = bp1$stats) bp2 <- boxplot(rbinom(60, 60, 0.5), col = "blue", main = ("Binomial Distribution of k=60 p=.5"), xlab = c(paste("p=0.5, mean", 60 * 0.5, sep = "="), paste("sd", sprintf("%0.2f", sqrt(60 * 0.5 * 0.5)), sep = "=")), ylab = "Value") points(x = 1, y = 60 * 0.5, pch = 8) text(1, 0.5 + 60 * 0.5, labels = 60 * 0.5) text(1.3, bp2$stats, labels = bp2$stats) bp3 <- boxplot(rbinom(60, 60, 0.7), col = "green", main = ("Binomial Distribution of k=60 p=.7"), xlab = c(paste("p=0.7, mean", 60 * 0.7, sep = "="), paste("sd", sprintf("%0.2f", sqrt(60 * 0.7 * 0.3)), sep = "=")), ylab = "Value") points(x = 1, y = 60 * 0.7, pch = 8) text(1, 0.5 + 60 * 0.7, labels = 60 * 0.7) text(1.3, bp3$stats, labels = bp3$stats)

 

boxplots

This entry was posted in Data Analytics and tagged , . Bookmark the permalink.

Leave a Reply