R Histograms & Box Plots
Bar charts compare categories. To see how a numeric variable is distributed, use a histogram or a box plot. Both answer: where are most values, how spread out are they, and are there unusual ones?
Histograms
A histogram divides the range of values into bins and draws a bar for how many values fall in each bin:
hist(airquality$Temp, col = "lightblue", border = "white",
main = "Daily maximum temperature, New York 1973",
xlab = "Temperature (°F)")
summary(airquality$Temp) Min. 1st Qu. Median Mean 3rd Qu. Max.
56.00 72.00 79.00 77.88 85.00 97.00
Controlling the bins
breaks sets the number of bins (as a suggestion) or the exact bin edges. Too few bins hide details; too many make noise. hist() also returns the counts, which you can inspect without drawing:
h <- hist(airquality$Temp, breaks = seq(55, 100, by = 5), plot = FALSE)
h$breaks
h$counts
hist(airquality$Temp, breaks = seq(55, 100, by = 5), col = "salmon",
main = "Bins of 5 °F", xlab = "Temperature (°F)") [1] 55 60 65 70 75 80 85 90 95 100
[1] 8 10 15 19 33 34 20 12 2
Density instead of counts
With freq = FALSE, the bar areas add up to 1, so you can overlay a smooth density curve or a theoretical distribution:
set.seed(10)
heights <- rnorm(300, mean = 170, sd = 8)
hist(heights, freq = FALSE, breaks = 20, col = "grey85",
main = "Simulated heights", xlab = "cm")
lines(density(heights), col = "blue", lwd = 2)
curve(dnorm(x, mean = 170, sd = 8), add = TRUE, col = "red", lty = 2, lwd = 2)
round(c(mean = mean(heights), sd = sd(heights)), 2) mean sd
169.46 7.69
Box plots
A box plot summarises a distribution in five numbers. The box spans the first to third quartile (the middle 50% of the data), the thick line is the median, the whiskers reach the most extreme values within 1.5 × the box length, and points beyond that are drawn individually as possible outliers.
boxplot(airquality$Ozone, horizontal = TRUE, col = "khaki",
main = "Ozone (ppb)", xlab = "ppb")
fivenum(airquality$Ozone)
boxplot.stats(airquality$Ozone)$out[1] 1.0 18.0 31.5 63.5 168.0
[1] 135 168
fivenum() returns the minimum, lower hinge, median, upper hinge and maximum (missing values are ignored). boxplot.stats() lists the points drawn as outliers.
Comparing groups
Box plots really shine when comparing groups side by side. Use the formula value ~ group:
boxplot(len ~ dose, data = ToothGrowth, col = c("#fde0dd", "#fa9fb5", "#c51b8a"),
main = "Tooth length by vitamin C dose", xlab = "Dose (mg/day)", ylab = "Length")
aggregate(len ~ dose, data = ToothGrowth, FUN = median) dose len
1 0.5 9.85
2 1.0 19.25
3 2.0 25.95
The higher the dose, the longer the teeth, and the boxes barely overlap: a strong hint that the difference is real. The Hypothesis Tests lesson shows how to check that formally.
Two grouping variables
boxplot(len ~ supp + dose, data = ToothGrowth,
col = c("orange", "lightgreen"), las = 2,
main = "By supplement and dose", xlab = "")
with(ToothGrowth, tapply(len, list(supp, dose), mean)) 0.5 1 2
OJ 13.23 22.70 26.06
VC 7.98 16.77 26.14
Test yourself
What does the thick line inside a box plot's box show?
- The mean
- The median
- The mode
- The maximum
Which argument of hist() controls the bins?
- bins
- breaks
- groups
- width
How do you draw one box per group of df$value by df$group?
- boxplot(df)
- boxplot(value ~ group, data = df)
- hist(value ~ group)
- barplot(df$group)