Created
November 3, 2020 21:47
-
-
Save andrewheiss/179171b792fb765a312203d68fa4b346 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(scales) | |
births_1994_1999 <- read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/births/US_births_1994-2003_CDC_NCHS.csv") %>% | |
# Ignore anything after 2000 | |
filter(year < 2000) | |
births_2000_2014 <- read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/births/US_births_2000-2014_SSA.csv") | |
births_combined <- bind_rows(births_1994_1999, births_2000_2014) | |
# The c() function lets us make a list of values | |
month_names <- c("January", "February", "March", "April", "May", "June", "July", | |
"August", "September", "October", "November", "December") | |
day_names <- c("Monday", "Tuesday", "Wednesday", | |
"Thursday", "Friday", "Saturday", "Sunday") | |
births <- births_combined %>% | |
# Make month an ordered factor, using the month_name list as labels | |
mutate(month = factor(month, labels = month_names, ordered = TRUE)) %>% | |
mutate(day_of_week = factor(day_of_week, labels = day_names, ordered = TRUE), | |
date_of_month_categorical = factor(date_of_month)) %>% | |
# Add a column indicating if the day is on a weekend | |
mutate(weekend = ifelse(day_of_week %in% c("Saturday", "Sunday"), TRUE, FALSE)) | |
avg_births_month_day_elections <- births %>% | |
filter(year %in% c(1996, 2000, 2004, 2008, 2012)) %>% | |
group_by(month, date_of_month_categorical) %>% | |
summarize(avg_births = mean(births)) | |
ggplot(data = avg_births_month_day_elections, | |
# By default, the y-axis will have December at the top, so use fct_rev() to reverse it | |
mapping = aes(x = date_of_month_categorical, y = fct_rev(month), fill = avg_births)) + | |
geom_tile() + | |
# Add viridis colors | |
scale_fill_viridis_c(option = "inferno", labels = comma) + | |
# Add nice labels | |
labs(x = "Day of the month", y = NULL, | |
title = "Average births per day", | |
subtitle = "Only years 1996, 2000, 2004, 2008, 2012", | |
fill = "Average births") + | |
# Force all the tiles to have equal widths and heights | |
coord_equal() + | |
# Use a cleaner theme | |
theme_minimal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment