---
title: "lab4_slides_supp"
author: "Brian Leung"
date: "`r Sys.Date()`"
output: pdf_document
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

# packages
library(tidyverse)
library(lubridate)
library(RColorBrewer)
library(ggrepel)
library(ggstance)
library(scales)

# theme
source("https://students.washington.edu/kpleung/vis/theme_cavis.R")

# load colors
colors <- brewer.pal(n = 5, name = "Set1")
blue <- colors[2]
red <- colors[1]
orange <- colors[5]
```

## Nobel prizes data: distribution

```{r}
# load data
nobel <- read_csv("data/nobel.csv")

# find top 5 countries
top5_country <- 
  nobel %>%
  count(organization_country) %>%
  drop_na() %>%
  slice_max(order_by = n, n = 5) %>%
  pull(organization_country)

# filter obs from top-5 countries
nobel_top5c <- 
  nobel %>%
  filter(organization_country %in% top5_country) %>%
  mutate(prize_date = paste0(prize_year, "12-10"),
         prize_date = ymd(prize_date))

# visualize
ggplot(nobel_top5c, aes(x = prize_date)) +
  theme_cavis_hgrid +
  geom_histogram() +
  scale_y_continuous(expand = c(0, 0), breaks = seq(0, 9, by = 3)) +
  scale_x_date(date_breaks = "25 years", date_labels = "%y") +
  facet_grid(category ~ organization_country) +
  #facet_wrap(category ~ organization_country) +
  labs(y = "Count", x = "Year of award") 

# a bit of wrangling
nobel_top5c_count <-
  nobel_top5c %>%
  mutate(decade = prize_year %/% 10 * 10) %>%
  group_by(decade, category, organization_country) %>%
  count() %>%
  filter(category != "Peace")

# visualize line plots
ggplot(nobel_top5c_count,
       aes(x = decade, y = n, color = organization_country)) +
  theme_cavis_hgrid +
  facet_wrap(~ category) +
  geom_line() +
  scale_color_brewer(type = "qual", palette = 6, direction = -1) +
  theme(legend.position = "top") +
  geom_point(color = "white", size = 4) +
  geom_point(size = 2) +
  labs(x = "Decade", y = "Decade avg. of awards")
```

## Nobel prizes data: time trend w/ prize shares

```{r}
# a bit of wrangling
nobel2 <- 
  nobel %>%
  mutate(prize_share2 = case_when(prize_share == "1/1" ~ 1,
                                  prize_share == "1/2" ~ 1 / 2,
                                  prize_share == "1/3" ~ 1 / 3,
                                  prize_share == "1/4" ~ 1 / 4),
         last_name = str_extract(full_name, "(?<=[ -])\\w+$"),    
         category = factor(category, 
                           levels = c("Chemistry", "Medicine", "Physics", "Peace", "Economics", "Literature"))
         ) 

# visualize
nobel2 %>%
  ggplot(aes(x = prize_year, y = prize_share2)) +
  theme_cavis_hgrid +
  geom_point(aes(color = gender, shape = gender)) +
  geom_text_repel(data = filter(nobel2, gender == "Female"), 
                  aes(label = last_name), color = orange, size = 3, max.overlaps = 20, box.padding = 0.5) +
  scale_shape_manual(values = c(1, 4)) +
  scale_color_manual(values = c(orange, blue)) +
  geom_smooth(color = "grey15", alpha = 0.2, size = 0.5, method = "gam") +
  scale_y_continuous(breaks = c(1/4, 1/3, 1/2, 1), labels = c("1/4", "1/3", "1/2", "1")) +
  facet_wrap(~ category) +
  theme(legend.position = "top") +
  labs(y = "Average prize share", x = "Year of award") 
```

## US Measles data 

```{r}
# load data
measles <- read_csv("data/measles.csv")

# line plots w/ facet_wrap
ggplot(measles, aes(x = year, y = count)) +
  theme_cavis_hgrid +
  facet_wrap(~ state) +
  geom_line() 
 
# coerce variables into factors
measles <- 
  measles %>%
  mutate(year = factor(year),
         state = factor(state),
         countCat = factor(countCat, 
                           levels = rev(c("0", "0-1", "1-10", "10-100", "100-500", "500-1000", ">1000"))))

# visualize in heatmap
ggplot(measles, aes(x = year, y = state, fill = countCat)) +
  theme_cavis +
  geom_tile(colour = "white", size = 0.25) +
  scale_fill_brewer(palette = "YlGnBu", direction = -1, na.value = "grey90") +
  scale_x_discrete(expand = c(0, 0), breaks = seq(1930, 2000, 10)) +
  scale_y_discrete(expand = c(0, 0)) +
  labs(y = NULL, x = NULL) +
  theme(axis.text.y = element_text(size = 8),
        axis.text.x = element_text(size = 8),
        legend.key.height = unit(0.8, "cm"),
        legend.key.width = unit(0.2, "cm"))
```

## 92 Presidential election data

```{r}
# load data
presVoteEV <- read_csv("data/presVoteEV.csv")

# make factors
presVoteEV <- 
  presVoteEV %>%
  mutate(
    nonwhite = if_else(nonwhite == 1, "Non-white", "White"),
    vote92 = factor(vote92, levels = c("Clinton", "Perot", "Bush"))
  )

# visualize
ggplot(presVoteEV,
       aes(x = rlibcon, y = pe, ymax = upper, ymin = lower,
           color = nonwhite, fill = nonwhite)) +
  theme_cavis_hgrid +
  facet_grid(~ vote92) +
  geom_line() +
  scale_color_manual(values = c(orange, blue)) +
  geom_ribbon(alpha = 0.25, linetype = 0, show.legend = FALSE) +
  scale_fill_manual(values = c(0, blue)) +
  geom_line(aes(y = upper, linetype = nonwhite)) +
  geom_line(aes(y = lower, linetype = nonwhite)) +
  scale_linetype_manual(values = c(2, 0)) +
  scale_x_continuous(breaks = seq(1, 7, 2)) +
  scale_y_continuous(labels = label_percent(), 
                     breaks = seq(0, 1, 0.25),
                     limits = c(0, 1), 
                     expand = c(0, 0)) +
  theme(legend.position = c(0.05, 0.15)) +
  labs(y = "Predicted prob. of voting",
       x = "Ideological self-placement")
```

## Cy Young data

```{r}
# load data
cyYoungFD <- read_csv("data/cyyoungFD.csv")

# a bit of wrangling
cyYoungFD <- 
  cyYoungFD %>%
  pivot_longer(cols = pe_m1:upper_m2, names_to = "col_names", values_to = "value") %>%
  separate(col_names, into = c("stat", "model"), sep = "_") %>%
  pivot_wider(names_from = stat, values_from = value) %>%
  mutate(signif = case_when(lower > 0 & upper > 0 ~ TRUE, 
                            lower < 0 & upper < 0 ~ TRUE, 
                            TRUE ~ FALSE))

# visualize a ropeladder
ggplot(cyYoungFD,
       aes(y = fct_reorder(covariate, pe, .desc = TRUE),
           x = pe, xmax = upper, xmin = lower,
           color = model, shape = signif, fill = signif)) +
  theme_cavis_vgrid +
  geom_pointrangeh(position = position_dodge2v(height = 0.5)) +                                           
  scale_colour_manual(values = c(blue, orange),
                      labels = c("Model 1", "Model 2")) + 
  scale_shape_manual(values = c(21, 19)) +        
  scale_fill_manual(values = c("white", NA)) +    
  guides(shape = "none", fill = "none") +         
  scale_x_continuous(sec.axis = dup_axis(name = NULL),
                     labels = label_percent()) +
  theme(legend.position = c(0.125, 0.125)) +
  labs(x = "Difference in predicted prob. in winning Cy Young", 
       y = NULL)
```





