library(tidyr)

library(tidyverse)

elect <- rio::import("https://github.com/marctrussler/IDS-Data/raw/main/VizData.Rds")
head(elect)

ggplot(elect, aes(x = p.white, y = plean.20)) 

ggplot(elect, aes(x = p.white, y = plean.20)) +
  geom_point()

ggplot(elect, aes(x = p.white, y = plean.20)) +
  geom_smooth()

ggplot(elect, aes(x = p.white, y = plean.20)) +
  geom_point() +
  geom_smooth()

elect |>
  filter(!is.na(plean.20)) -> elect.2

ggplot(elect.2, aes(x = p.white, y = plean.20)) +
  geom_point()

#Notive we don't specify the dataset in the ggplot command. It's just going to use the piped in dataset
elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = p.white, y = plean.20)) +
  geom_point()

elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = p.white, y = plean.20)) +
  geom_point() +
  labs(x = "Percent White",
       y = "Partisan Lean (2020)",
       title = "Whiter Districts Lean More Republican") +
  theme_minimal()

elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = p.white, y = plean.20,
             color = as.factor(highly.educated))) +
  geom_point() +
  scale_color_manual(values = c("firebrick", "dodgerblue"),
                     labels = c("Below Median", "Above Median")) +
  labs(x = "Percent White",
       y = "Partisan Lean (2020)",
       color = "Bachelor's Degree Rate",
       title = "Whiter Districts Lean More Republican") +
  theme_minimal()

#Fixed color: goes outside aes()
elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = p.white, y = plean.20)) +
  geom_point(color = "steelblue")

#Wrong: trying to put a fixed color in the aes()
elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = p.white, y = plean.20,color = "steelblue")) +
  geom_point()

elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = p.white, y = plean.20,
             color = as.factor(highly.educated))) +
  geom_point(alpha = 0.3) +
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("firebrick", "dodgerblue"),
                     labels = c("Below Median", "Above Median")) +
  labs(x = "Percent White", y = "Partisan Lean (2020)",
       color = "Bachelor's Degree Rate") +
  theme_minimal()

elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = p.white, y = plean.20)) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey50") +
  geom_point(alpha = 0.3) +
  geom_smooth(method = "lm", color = "firebrick") +
  labs(x = "Percent White", y = "Partisan Lean (2020)") +
  theme_minimal()

elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = p.white, y = plean.20)) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey50") +
  geom_point(alpha = 0.4) +
  geom_smooth(method = "lm", color = "firebrick", se = FALSE) +
  facet_wrap(~region) +
  labs(x = "Percent White", y = "Partisan Lean (2020)",
       title = "Partisan Lean by Racial Composition, by Region") +
  theme_minimal()

elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = plean.20)) +
  geom_histogram(binwidth = 5, fill = "steelblue", color = "white") +
  labs(x = "Partisan Lean (2020)", y = "Number of Districts") +
  theme_minimal()

elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = plean.20, fill = region)) +
  geom_density(alpha = 0.4) +
  labs(x = "Partisan Lean (2020)", fill = "Region") +
  theme_minimal()

elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = plean.20, fill=region)) +
  geom_density(alpha = 0.4) +
  facet_wrap(~region)+ 
  labs(x = "Partisan Lean (2020)", fill = "Region") +
  theme_minimal()

elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = plean.20, fill=region)) +
  geom_density(alpha = 0.4) +
  facet_wrap(~region)+ 
  labs(x = "Partisan Lean (2020)", fill = "Region") +
  theme_minimal()+
  theme(legend.position = "none")


elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = region, y = plean.20, fill = region)) +
  geom_boxplot() +
  labs(x = "", y = "Partisan Lean (2020)") +
  theme_minimal() +
  theme(legend.position = "none")

elect |>
  filter(!is.na(plean.20)) |>
  group_by(region) |>
  summarize(mean.lean = mean(plean.20)) |>
  ggplot(aes(x = region, y = mean.lean)) +
  geom_col(fill = "steelblue") +
  labs(x = "", y = "Mean Partisan Lean (2020)") +
  theme_minimal()

elect |>
  filter(!is.na(pshift.16to20)) |>
  group_by(region, highly.educated) |>
  summarize(mean.shift = mean(pshift.16to20), .groups = "drop") |>
  ggplot(aes(x = region, y = mean.shift,
             fill = as.factor(highly.educated))) +
  geom_col(position = "dodge") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  scale_fill_manual(values = c("firebrick", "dodgerblue"),
                    labels = c("Below Median", "Above Median")) +
  labs(x = "", y = "Mean Partisan Shift (2016 to 2020)",
       fill = "Bachelor's Degree Rate") +
  theme_minimal()

elect |>
  filter(!is.na(pshift.16to20)) |>
  group_by(region, highly.educated) |>
  summarize(mean.shift = mean(pshift.16to20), .groups = "drop") |>
  ggplot(aes(x = region, y = mean.shift,
             fill = as.factor(highly.educated))) +
  geom_col() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  scale_fill_manual(values = c("firebrick", "dodgerblue"),
                    labels = c("Below Median", "Above Median")) +
  labs(x = "", y = "Mean Partisan Shift (2016 to 2020)",
       fill = "Bachelor's Degree Rate") +
  theme_minimal()

elect |>
  filter(!is.na(pshift.16to20)) |>
  group_by(region) |>
  summarize(mean.shift = mean(pshift.16to20)) |>
  ggplot(aes(x = fct_reorder(region, mean.shift), y = mean.shift)) +
  geom_col(fill = "steelblue") +
  labs(x = "", y = "Mean Partisan Shift (2016 to 2020)") +
  theme_minimal()

my.plot <- elect |>
  filter(!is.na(plean.20)) |>
  ggplot(aes(x = p.white, y = plean.20)) +
  geom_point(alpha = 0.3) +
  labs(x = "Percent White", y = "Partisan Lean (2020)") +
  theme_minimal()

ggsave("my_figure.png", my.plot, width = 6, height = 4, units = "in")
