New Zealand Bird of the Year

Time series plots of when people voted for the New Zealand bird of the year

Roupen Khanjian
11-19-2019
# Tidy Tuesday 11/19/2019
# NZ Bird of the Year
# Roupen Khanjian

library(tidyverse) # Easily Install and Load the 'Tidyverse' 
library(lubridate) # Make Dealing with Dates a Little Easier
library(ggthemes) # Extra Themes, Scales and Geoms for 'ggplot2'
library(patchwork) # The Composer of Plots
library(wesanderson) # A Wes Anderson Palette Generator
nz_bird <- readr::read_csv(
  "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-11-19/nz_bird.csv")

nz_bird <- nz_bird %>% 
  dplyr::filter(complete.cases(bird_breed) == TRUE) %>% # Remove missing votes
  mutate(Rank = fct_infreq(vote_rank, ordered = TRUE)) %>% # Convert vote_rank into a factor
  mutate(Rank = str_replace(Rank, "vote_", "")) %>% # Change vote column strings. 
  dplyr::select(-vote_rank) %>% 
  unite("time", date:hour, remove = FALSE) %>% # Combine date and hour to a new column named time
  mutate(time = ymd_h(time)) %>% # Convert time to date-times type
  mutate(weekday =  wday(date, label = TRUE, abbr = FALSE)) # Add the weekday 

# Get top 5 birds
nz_bird_top5 <- nz_bird %>% 
  count(bird_breed, sort = TRUE) %>% 
  slice(1:5) 

# Get top 5 birds for my data frame
nz_bird_join <- nz_bird %>% 
  inner_join(nz_bird_top5, by = "bird_breed") %>% 
  arrange(desc(n))

# Color palette
my_colors <-  wes_palette(name = "Darjeeling1", n = 5, type = "discrete")

p1 <- nz_bird_join %>% 
  group_by(time, bird_breed) %>% 
  count(time) %>% 
  arrange(desc(n)) %>% 
  ggplot(aes(x = time, y = n, fill = bird_breed)) +
  geom_col(alpha = 0.95) + 
  scale_x_datetime(date_breaks = "2 days", date_labels = "%b %d") +
  scale_y_continuous(breaks = seq(0, 500, by = 100)) +
  labs(x = "",y = "Frequency", title = "Hourly Voting",
       fill = "Bird Breed") +
  theme_bw() +
  scale_fill_manual(values = my_colors)  +
  theme(title = element_text(face="bold", size=9),
        axis.text.y = element_text(face="bold", size=11, angle = 10),
        axis.text.x =  element_text(face="bold", size=11, angle = 10),
        axis.title.x = element_text(face = "bold", size = 13),
        axis.title.y = element_blank(),
        legend.position = "bottom",
        panel.background = element_rect(fill = "white", colour = "black"),
        legend.direction = "horizontal")

week_levels <- levels(nz_bird_join$weekday) # extract weekdays
week_levels <- str_sub(week_levels, start = 1L, end = 3L) # extract first 3 letters

p4 <- nz_bird_join %>% 
  mutate(weekday = str_sub(weekday,
                           start = 1L, end = 3L)) %>%
  mutate(weekday = factor(weekday, levels = week_levels,
                          ordered = TRUE)) %>% 
  group_by(weekday, hour, bird_breed) %>% 
  count(hour) %>% 
  arrange(desc(n)) %>%
  ggplot(aes(x = hour, y = n, fill = bird_breed)) +
  geom_col() + 
  facet_grid(weekday ~ .) +
  scale_x_continuous(breaks = seq(0, 23, by = 2)) +
  scale_y_continuous(breaks = seq(0, 800, by = 400)) +
  labs(x = "Time of day", y = "Frequency",
       fill = "Bird Breed") +
  scale_fill_manual(values = my_colors)  +
  theme_bw() +
  theme(axis.text.y = element_text(face="bold", size=9, angle = 10),
        axis.text.x =  element_text(face="bold", size=11, angle = 10),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.background = element_rect(fill = "white",colour = "black"),
        strip.text = element_text(face="bold", size=9),
        strip.background = element_rect(fill = "white"),
        legend.text = element_text(color = "black", face = "bold", size = 7.75),
        legend.position = "none",
        legend.key.size = unit(0.255, units = "cm"),
        legend.background = element_rect(fill = "white"),
        plot.background = element_rect(fill = "white",
                                       colour = "white"))

p3 <- nz_bird_join %>% 
  group_by(hour, bird_breed) %>% 
  count(hour) %>% 
  ggplot(aes(x = hour, y = n, fill = bird_breed)) +
  geom_col(alpha = 0.95) + 
  scale_x_continuous(breaks = seq(0, 23, by = 2)) +
  scale_y_continuous(breaks = seq(0, 16000, by = 1000)) +
  labs(x = "", y = "Frequency", title = "Hour of Day Voting Occured") +
  theme_bw() +
  scale_fill_manual(name = "Bird Breed", values = my_colors) +
  theme(title = element_text(face="bold", size=9),
        axis.text.y = element_text(face="bold", size=11, angle = 10),
        axis.text.x = element_text(face="bold", size=11, angle = 10),
        axis.title.y = element_blank(),
        legend.position = "bottom",
        panel.background = element_rect(fill = "white",colour = "black"),
        legend.direction = "horizontal")

p2 <- nz_bird_join %>% 
  group_by(weekday, bird_breed) %>% 
  count(weekday) %>% 
  arrange(desc(n)) %>% 
  ggplot(aes(x = weekday, y = n, fill = bird_breed)) +
  geom_col(alpha = 0.95) + 
  scale_y_continuous(breaks = seq(0, 40000, by = 2500)) +
  labs(x = "",y = "Frequency", title = "Day of Week Voting Occured") +
  theme_bw() +
  scale_fill_manual(values = my_colors) +
  theme(title = element_text(face="bold", size=9),
        axis.text.y = element_text(face="bold", size=11, angle = 10),
        axis.text.x =  element_text(face="bold", size=10.5, angle = 13),
        axis.title.y = element_blank(),
        axis.title.x = element_text(face = "bold", size = 13),
        legend.position = "none",
        panel.background = element_rect(fill = "white", colour = "black"))

# Plot 1
p1 +
  plot_annotation(
    title = "Tidy Tuesday: NZ Bird of the Year",
    subtitle = "When did people vote for the top 5 bird breeds?",
    theme = theme(plot.title = element_text(face = "bold", size = 15),
                  plot.subtitle = element_text( size = 12)))

Citation

For attribution, please cite this work as

Khanjian (2019, Nov. 19). Roupen Khanjian: New Zealand Bird of the Year. Retrieved from https://khanjian.github.io/roupen-website/tidytuesday/nz_bird/

BibTeX citation

@misc{khanjian2019new,
  author = {Khanjian, Roupen},
  title = {Roupen Khanjian: New Zealand Bird of the Year},
  url = {https://khanjian.github.io/roupen-website/tidytuesday/nz_bird/},
  year = {2019}
}