Files
visual-data/VT2-examples/vt2-ch8-codedata-dir/make-histogram.R
T

71 lines
1.3 KiB
R

library(readr)
library(dplyr)
#
# Load data
#
players <- read_csv("data/nba-players-2013-14.csv")
#
# Histogram
#
# Default histogram
par(las = 1)
hist(players$Ht_inches,
xlab="inches",
main="NBA Player Heights")
# Smaller breaks
hist(players$Ht_inches,
breaks=seq(65, 90, 1),
xlab="inches",
main="NBA Player Heights")
# Vary bin sizes
par(mfrow=c(1,3), mar=c(5,3,3,3))
hist(players$Ht_inches,
breaks=seq(65, 90, 1),
xlab="inches", main="One-inch bins")
hist(players$Ht_inches,
breaks=seq(65, 90, 3),
xlab="inches", main="Three-inch bins")
hist(players$Ht_inches,
breaks=seq(65, 90, 6),
xlab="inches", main="Six-inch bins")
#
# Comparison between positions
#
ppos <- unique(players$POS)
par(mfrow=c(5,1), mar=c(2,3,3,2), oma=c(2,2,2,2))
for (pos in ppos) {
curr <- players %>% filter(POS == pos)
hist(curr$Ht_inches,
breaks=seq(65, 90, 1),
xlab="inches", main=paste0(pos, " Heights"))
}
ppos2 <- c("G", "G/F", "F", "F/C", "C")
par(mfrow=c(5,1))
for (pos in ppos2) {
curr <- players %>% filter(POS == pos)
curr_mean <- mean(curr$Ht_inches)
hist(curr$Ht_inches,
breaks=seq(65, 90, 1),
border = "white",
xlab="inches",
main=paste0(pos, " Heights"))
abline(v = curr_mean,
col = "blue",
lwd = 2)
}