Presidential Baseball
Create a factor called election in the Teams data set that divides the yearID into four-year blocks that correspond to U.S. presidential terms. During which term have the most home runs been hit? Start with the presidential term 1869-1872 leading to the present.
Modifying the Data Set
yearBreaks <- seq(1869,2017, by = 4)
TeamElectionYears <-
Teams %>%
mutate(election =
cut(yearID, breaks = yearBreaks, right = FALSE))Graph
ggplot(TeamElectionYears, aes(x = election, y = HR)) +
geom_point() +
theme(axis.text.x = element_text(angle = 75, hjust = 1),
plot.title = element_text(hjust = 0.5)) +
labs(x = "US Presidential Terms", y = "Home Runs Hit",
title = "Home Runs v. US Presidential Years")Even using the element_text() function to alter the \(x\)-axis labels, the \(x\)-axis is too busy to easily determine who hit the most home runs during a term. So let’s break the data down further into the top 50 terms with the most home runs:
TeamElectionYears %>%
arrange(desc(HR)) %>%
head(50) %>%
ggplot(aes(x = election, y = HR)) +
geom_point() +
theme(axis.text.x = element_text(angle = 75, hjust = 1),
plot.title = element_text(hjust = 0.5)) +
labs(x = "US Presidential Terms", y = "Home Runs Hit",
title = "Home Runs v. US Presidential Years")Using this second graph, it’s easier to determine that the US presidential term with the most homeruns hit is 1997 to 2001, during Bill Clinton’s presidency.