NL East Teams Games above .500 in 2019
Data
This plot uses the nleast data frame of the gcubed package. The original data was obtained from Baseball Reference. Some data wrangling has been done to add the last 6 columns. In particular the games_updown variable is the y-variable in this plot (the number of wins - the number of losses for team in that row after the game).
A small subset of the data can be seen below:
library(gcubed)
nleast[c(1,125,250,400,500), c(2,3,22)]
## # A tibble: 5 x 3
## Date Tm games_updown
## <chr> <fct> <dbl>
## 1 Thursday Mar 28 ATL -1
## 2 Friday Apr 5 PHI 4
## 3 Sunday Apr 14 WSN 0
## 4 Wednesday May 22 NYM -2
## 5 Friday May 3 MIA -13
Some data wrangling code
First split the date into Weekday, Month and Day.
library(tidyr)
library(dplyr)
Month = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
month_num_table <- data.frame(Month, MonthNum = 1:12)
df <- separate(nleast, Date, into = c("WeekDay", "Month", "Day"), sep = " ") %>%
mutate(Day = as.integer(Day))
Create a variable named MonthDay to sort the games by:
df <- left_join(df, month_num_table, by = "Month") %>%
mutate(MonthNum = sprintf("%02d", as.numeric(MonthNum)),
Day = sprintf("%02d", as.numeric(Day)),
MonthDay = paste(MonthNum, Day, sep = "-")) %>%
arrange(MonthDay)
Code for plot
library(ggplot2)
df$MonthDay <- factor(df$MonthDay)
games_500_plot <- ggplot(data = df, aes(x = MonthDay, y = games_updown, group = Tm, colour = Tm)) +
geom_step() +
theme_bw() +
geom_vline(xintercept = 5, linetype = "dashed", colour = "grey") +
geom_vline(xintercept = 35, linetype = "dashed", colour = "grey") +
geom_vline(xintercept = 66, linetype = "dashed", colour = "grey") +
geom_vline(xintercept = 96, linetype = "dashed", colour = "grey") +
geom_vline(xintercept = 122, linetype = "dashed", colour = "grey") +
geom_text(aes(10,22,label = "April"), colour = "grey") +
geom_text(aes(40,22,label = "May"), colour = "grey") +
geom_text(aes(71,22,label = "June"), colour = "grey") +
geom_text(aes(101,22,label = "July"), colour = "grey") +
ylab("Games above .500") +
ggtitle("NL East 2019") +
theme(axis.title.x=element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.title = element_blank(),
plot.title = element_text(size = 16, face = "bold", hjust = 0.5))
games_500_plot