#Title: dwv_ps6_ms_5_ggplot2.R
#Author: Jack Reilly
#Purpose: Load data
# requires: working directory, libraries, gss_collapse
# result: fig1, 2, 5 in ggplot2 (to viewer and wd)

##Let's do it in GGPlot

library(ggplot2)
library(grid)  # for unit() in arrow()

v77   <- gss_collapse$stouffer[gss_collapse$year == 1977]
v98   <- gss_collapse$stouffer[gss_collapse$year == 1998]
v21 <- gss_collapse$stouffer[gss_collapse$year == 2021]

fig1 <- ggplot(gss_collapse, aes(x = year, y = stouffer)) +
  geom_line(linewidth = 1) +
  scale_x_continuous(limits = c(1976, 2021), breaks = c(1976, 2021)) +
  scale_y_continuous(limits = c(0, 15), breaks = c(0, 15)) +
  labs(
    x = "Year",
    y = "Stouffer",
    title = "Figure 1. The 0-15 GSS Modified Stouffer Tolerance Battery, 1976-2021"
  ) +
  # text labels
  annotate("text", x = 1985, y = 8.0,  label = paste(round(v77, 2),   "(1977)")) +
  annotate("text", x = 1990, y = 3.5,  label = paste(round(v98, 2),   "(1998)")) +
  annotate("text", x = 2014, y = 3.1,  label = paste(round(v21, 2), "(2021)")) +
  # arrows (heads at xend,yend like code=2)
  annotate("segment",
           x = 1981.5, y = 7.8, xend = 1977.5, yend = 7.4,
           arrow = arrow(length = unit(0.1, "in"), angle = 30, ends = "last"),
           linewidth = 0.6, colour = "black") +
  annotate("segment",
           x = 1993.5, y = 3.7, xend = 1997.5, yend = 5.0,
           arrow = arrow(length = unit(0.1, "in"), angle = 30, ends = "last"),
           linewidth = 0.6, colour = "black") +
  annotate("segment",
           x = 2017.0, y = 3.5, xend = 2020.5, yend = 4.3,
           arrow = arrow(length = unit(0.1, "in"), angle = 30, ends = "last"),
           linewidth = 0.6, colour = "black") +
  theme_minimal(base_size = 12) +
  theme(panel.grid.minor = element_blank())

fig1
ggsave("figures/ggplot/figure1.pdf", fig1, width = 7.5, height = 7.55, dpi = 300)
ggsave("figures/ggplot/figure1.png", fig1, width = 7.5, height = 7.55, dpi = 300)

##Figure 2

fig2<-ggplot(gss_collapse, aes(x = year)) +
  # two lines
  geom_line(aes(y = rac), linewidth = 1) +
  geom_line(aes(y = hom), linewidth = 1, linetype = "dashed") +
  # scales with only endpoints / custom breaks
  scale_x_continuous(limits = c(1976, 2021), breaks = c(1976, 2021)) +
  scale_y_continuous(limits = c(0, 3), breaks = c(0, 1, 2, 3)) +
  # labels
  labs(
    x = NULL,
    y = NULL,
    title = "Figure 2. Intolerance Toward Racists and Homosexuals, 1976-2021"
  ) +
  # text annotations
  annotate("text", x = 1980, y = 1.5, label = "Racists") +
  annotate("text", x = 1989, y = 0.7, label = "Homosexuals") +
  theme_minimal(base_size = 12) +
  theme(panel.grid.minor = element_blank())

fig2
ggsave("figures/ggplot/figure2.pdf", fig2, width = 7.5, height = 7.55, dpi = 300)
ggsave("figures/ggplot/figure2.png", fig2, width = 7.5, height = 7.55, dpi = 300)


## Figure 5

fig5<-ggplot(gss_collapse, aes(x = year, y = intol)) +
  geom_line(linewidth = 1.2) +
  scale_x_continuous(limits = c(1976, 2021), breaks = c(1976, 2021)) +
  scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, by = 0.1)) +
  labs(
    x = NULL,
    y = NULL,
    title = "Figure 5. A Dichotomous Measure of Tolerance and Intolerance, 1976-2021"
  ) +
  theme_minimal(base_size = 12) +
  theme(panel.grid.minor = element_blank())

fig5
ggsave("figures/ggplot/figure5.pdf", fig5, width = 7.5, height = 7.55, dpi = 300)
ggsave("figures/ggplot/figure5.png", fig5, width = 7.5, height = 7.55, dpi = 300)