#Title: DWV Assign 6 - Mondak Sanders.R
#Author: Jack Reilly
#Purpose: Replicate Figures 1, 2, and 5 from Mondak & Sanders
#Requires: 1972-2024 Cumulative GSS Data ("gss7224_r1.dta")
#Output: Figures as PDFs (ggplot only)

library(tidyverse)
library(haven)

setwd("~/desktop/working/dwv/mondaksanders2026")

gss<-read_dta("gss7224_r1.dta")

#Select the variables used by Mondak and Sanders, including the Year variable
#Mechanism: some of these show up in the codebook! But others don't.  
#Once you see the pattern, the rest are straightforward; missing ones can be found noted in GSS2024 Release Variables as dropped for the 2024 year
gss_sub <- gss |> select(year, spkath, colath, libath, spkrac, colrac, librac, spkcom, colcom, libcom, spkmil, colmil, libmil, spkhomo, colhomo, libhomo)

gss_sub <- filter(gss_sub, year >= 1976)

table(gss_sub$year)

table(gss_sub$colath)
unique(gss_sub$spkath)

gss_sub <- gss_sub |>
  mutate(
    ath1  = case_when(spkath == 1 ~ 0,
                      spkath == 2 ~ 1),
    ath2  = case_when(colath == 4 ~ 0,
                      colath == 5 ~ 1),
    ath3  = case_when(libath == 2 ~ 0,
                      libath == 1 ~ 1),
    
    rac1  = case_when(spkrac == 1 ~ 0,
                      spkrac == 2 ~ 1),
    rac2  = case_when(colrac == 4 ~ 0,
                      colrac == 5 ~ 1),
    rac3  = case_when(librac == 2 ~ 0,
                      librac == 1 ~ 1),
    
    com1  = case_when(spkcom == 1 ~ 0,
                      spkcom == 2 ~ 1),
    com2  = case_when(colcom == 5 ~ 0,   # note: colcom 4=1, 5=0
                      colcom == 4 ~ 1),
    com3  = case_when(libcom == 2 ~ 0,
                      libcom == 1 ~ 1),
    
    mil1  = case_when(spkmil == 1 ~ 0,
                      spkmil == 2 ~ 1),
    mil2  = case_when(colmil == 4 ~ 0,
                      colmil == 5 ~ 1),
    mil3  = case_when(libmil == 2 ~ 0,
                      libmil == 1 ~ 1),
    
    hom1 = case_when(spkhomo == 1 ~ 0,
                      spkhomo == 2 ~ 1),
    hom2 = case_when(colhomo == 4 ~ 0,
                      colhomo == 5 ~ 1),
    hom3 = case_when(libhomo == 2 ~ 0,
                      libhomo == 1 ~ 1)
  )

#Add all the variables together to make stouffer for fig1
gss_sub$stouffer <- gss_sub$ath1 + gss_sub$ath2 + gss_sub$ath3 + gss_sub$rac1 + gss_sub$rac2 + gss_sub$rac3 + gss_sub$com1 + gss_sub$com2 + gss_sub$com3 + gss_sub$mil1 + gss_sub$mil2 + gss_sub$mil3 + gss_sub$hom1 + gss_sub$hom2 + gss_sub$hom3
gss_sub$stouffer2 <- rowSums(gss_sub[, c("ath1", "ath2", "ath3", "rac1", "rac2", "rac3", "com1", "com2", "com3", "mil1", "mil2", "mil3", "hom1", "hom2", "hom3")], na.rm = TRUE)
gss_sub$stouffer3 <- rowSums(gss_sub[, c("ath1", "ath2", "ath3", "rac1", "rac2", "rac3", "com1", "com2", "com3", "mil1", "mil2", "mil3", "hom1", "hom2", "hom3")])

cor(gss_sub$stouffer, gss_sub$stouffer2, use = "pairwise.complete.obs")
cor(gss_sub$stouffer, gss_sub$stouffer3, use = "pairwise.complete.obs")
cor(gss_sub$stouffer2, gss_sub$stouffer3, use = "pairwise.complete.obs")

sum(!is.na(gss_sub$stouffer))
sum(!is.na(gss_sub$stouffer2))
sum(!is.na(gss_sub$stouffer3))

#What am I going to do here?
#Missing data management is important - here, I'm going to decide that I *just* want to have observations where I have complete data
#So I'm going to use the first or third commands

#Add rac and hom variables together for fig 2
gss_sub$rac = gss_sub$rac1 + gss_sub$rac2 + gss_sub$rac3
gss_sub$hom = gss_sub$hom1 + gss_sub$hom2 + gss_sub$hom3

#Generate intolerance variables - keep 0 as 0; keep missing as missing, else; turn into 1
gss_sub <- gss_sub |>
  mutate(
    intol = case_when(
      stouffer == 0 ~ 0,
      stouffer %in% 1:15 ~ 1  # everything else
    )
  )

#Look at our results
table(gss_sub$stouffer)
table(gss_sub$rac)
table(gss_sub$hom)
table(gss_sub$intol)

gss_collapse <- gss_sub |>
  group_by(year) |>
  summarize(
    stouffer = mean(stouffer, na.rm = T),
    rac = mean(rac, na.rm=T),
    hom = mean(hom, na.rm=T),
    intol = mean(intol, na.rm=T)
    )

table(gss_collapse$stouffer)
table(gss_collapse$rac)
table(gss_collapse$hom)
table(gss_collapse$intol)


plot(gss_collapse$year, gss_collapse$stouffer)
plot(gss_collapse$year, gss_collapse$rac)
plot(gss_collapse$year, gss_collapse$hom)
plot(gss_collapse$year, gss_collapse$intol)

#OK! We've made it to our first shot at the graphs.  Now it's time to correct some issues:

#Figure 1
#Make it nicer looking

plot(gss_collapse$year, gss_collapse$stouffer, type = "o", col = "blue",
     xlab = "Year", ylab = "Stouffer",
     main = "Mondak & Sanders Replication")

#Huh. There are some missing years
gss_collapse <- gss_collapse |>
  filter(!year %in% c(1978, 1983, 1986))

#Let's do it a little bit nicer
plot(gss_collapse$year, gss_collapse$stouffer, type = "l", col = "blue",
     xlab = "Year", ylab = "Stouffer",
     xlim = c(1976, 2021), ylim = c(0, 15),
     main = "Figure 1. The 0-15 GSS Modified Stouffer Tolerance Battery, 1976-2021")

#Let's do it properly nicer, with layering in base R

plot(x=0, type='n',
     xlim=c(1976, 2021), xaxt = "n",
     ylim=c(0,15), yaxt = "n",
     xlab="Year", 
     ylab="Stouffer", 
     main="Figure 1. The 0-15 GSS Modified Stouffer Tolerance Battery, 1976-2021")

axis(1, at = c(1976, 2021)) #1 says "bottom axis"
axis(2, at = c(0, 15)) #2 says "left axis"

lines(gss_collapse$year, gss_collapse$stouffer, lwd=2)
#points(gss_collapse$year, gss_collapse$stouffer)

#Let's put those arrows on anyways!
#use to check positioning
#text(1985, 8, "I want it that way", col = "firebrick3")


#Actual
#text(1985, 8, "I want it that way", col = "firebrick3")
#text(1990, 3.5, "I want it that way", col = "firebrick3")
#text(2010, 2.5, "I want it that way", col = "firebrick3")

#This is kind of a clunky procedure anwyays. So I'm not going to worry about doing this in an internal, "pull the number from here" way
#I'm just going to look it up and put it in
print(gss_collapse, n=30)

#1977 is 6.84!
#1998 is 4.61!
#2021 is 4.41!

text(1985, 8, "6.84 (1977)")
text(1990, 3.5, "4.61 (1998)")
text(2014, 3.1, "4.41 (2021)")

#alt, if I *really* wanted to
v77<-gss_collapse$stouffer[gss_collapse$year == 1977]
v98<-gss_collapse$stouffer[gss_collapse$year == 1998]
v21<-gss_collapse$stouffer[gss_collapse$year == 2021]

#I can use the paste command to stick things together in a string
#text(1985, 8, paste(v77, "(1977)"))

#Oops.  Let's not have so many digits.  We'll round
text(1985, 8, paste(round(v77, 2), "(1977)"))
text(1990, 3.5, paste(round(v98, 2), "(1998)"))
text(2014, 3.1, paste(round(v21, 2), "(2021)"))

#Let's get those arrows on

#Practice
#arrows(x0 = 1985, y0=9, x1=1984, y1=10,
#       length = 0.1,  # length of arrowhead
#       angle  = 30,   # angle of arrowhead
#       code   = 2,    # 1 = head at x0,y0; 2 = head at x1,y1; 3 = both
#       col    = "red", lwd = 2)

arrows(x0 = 1981.5, y0=7.8, x1=1977.5, y1=7,
       length = 0.1,  # length of arrowhead
       angle  = 30,   # angle of arrowhead
       code   = 2,    # 1 = head at x0,y0; 2 = head at x1,y1; 3 = both
       col    = "black", lwd = 1)

arrows(x0 = 1993.5, y0=3.7, x1=1997.5, y1=4.5,
       length = 0.1,  # length of arrowhead
       angle  = 30,   # angle of arrowhead
       code   = 2,    # 1 = head at x0,y0; 2 = head at x1,y1; 3 = both
       col    = "black", lwd = 1)

arrows(x0 = 2017, y0=3.2, x1=2020.5, y1=4.0,
       length = 0.1,  # length of arrowhead
       angle  = 30,   # angle of arrowhead
       code   = 2,    # 1 = head at x0,y0; 2 = head at x1,y1; 3 = both
       col    = "black", lwd = 1)

#Haha! 

#Figure 2
plot(gss_collapse$year, gss_collapse$rac, type = "l", col = "blue",
     xlab = "Year", ylab = "Stouffer",
     xlim = c(1976, 2021), ylim = c(0, 3),
     main = "Figure 2. Intolerance Toward Racists and Homosexuals, 1976-2021")

plot(gss_collapse$year, gss_collapse$hom, type = "l", col = "blue",
     xlab = "Year", ylab = "Stouffer",
     xlim = c(1976, 2021), ylim = c(0, 3),
     main = "Figure 2. Intolerance Toward Racists and Homosexuals, 1976-2021")

#Layer time
plot(x=0, type='n',
     xlim=c(1976, 2021), xaxt = "n",
     ylim=c(0,3), yaxt = "n",
     xlab=" ", 
     ylab=" ", 
     main="Figure 2. Intolerance Toward Racists and Homosexuals, 1976-2021")

axis(1, at = c(1976, 2021)) #1 says "bottom axis"
axis(2, at = c(0, 1, 2, 3)) #2 says "left axis"

lines(gss_collapse$year, gss_collapse$rac, lwd=2)
#lines(gss_collapse$year, gss_collapse$hom, lwd=2)
lines(gss_collapse$year, gss_collapse$hom, lwd=2, lty=2)

#Oh no, now all the text

text(1980, 1.5, "Racists")
text(1989, .7, "Homosexuals")


#Figure 5
plot(gss_collapse$year, gss_collapse$intol, type = "l", col = "blue",
     xlab = "Year", ylab = "Stouffer",
     xlim = c(1976, 2021), ylim = c(0, 1),
     main = "Figure 5. A Dichotomous Measure of Tolerance and Intolerance, 1976-2021")

#Layer time
plot(x=0, type='n',
     xlim=c(1976, 2021), xaxt = "n",
     ylim=c(0,1), yaxt = "n",
     xlab=" ", 
     ylab=" ", 
     main="Figure 5. A Dichotomous Measure of Tolerance and Intolerance, 1976-2021")

axis(1, at = c(1976, 2021)) #1 says "bottom axis"
axis(2, at = seq(0, 1, by = 0.1)) #2 says "left axis"

lines(gss_collapse$year, gss_collapse$intol, lwd=2)


#So what haven't we done yet?
#Missing data - there are about 400 NA(d) in the spkath, for instance
table(na_tag(gss_sub$spkath))

gss_sub <- gss |> select(year, spkath, colath, libath, spkrac, colrac, librac, spkcom, colcom, libcom, spkmil, colmil, libmil, spkhomo, colhomo, libhomo)

#There is also an experiment the gss starts running with naming of these variables after 2021
#We'll ignore that for now and just look up to 2021

gss_sub <- filter(gss_sub, year >= 1976 & year <= 2021)

table(gss_sub$year)

gss_sub <- gss_sub |>
  mutate(
    ath1  = case_when(spkath == 1 ~ 0,
                      spkath == 2 ~ 1,
                      is_tagged_na(spkath) & na_tag(spkath) == "d" ~ 1),
    ath2  = case_when(colath == 4 ~ 0,
                      colath == 5 ~ 1,
                      is_tagged_na(colath) & na_tag(colath) == "d" ~ 1),
    ath3  = case_when(libath == 2 ~ 0,
                      libath == 1 ~ 1,
                      is_tagged_na(libath) & na_tag(libath) == "d" ~ 1),
    
    rac1  = case_when(spkrac == 1 ~ 0,
                      spkrac == 2 ~ 1,
                      is_tagged_na(spkrac) & na_tag(spkrac) == "d" ~ 1),
    rac2  = case_when(colrac == 4 ~ 0,
                      colrac == 5 ~ 1,
                      is_tagged_na(colrac) & na_tag(colrac) == "d" ~ 1),
    rac3  = case_when(librac == 2 ~ 0,
                      librac == 1 ~ 1,
                      is_tagged_na(librac) & na_tag(librac) == "d" ~ 1),
    
    com1  = case_when(spkcom == 1 ~ 0,
                      spkcom == 2 ~ 1,
                      is_tagged_na(spkcom) & na_tag(spkcom) == "d" ~ 1),
    com2  = case_when(colcom == 5 ~ 0,   # note: colcom 4=1, 5=0
                      colcom == 4 ~ 1,
                      is_tagged_na(colcom) & na_tag(colcom) == "d" ~ 1),
    com3  = case_when(libcom == 2 ~ 0,
                      libcom == 1 ~ 1,
                      is_tagged_na(libcom) & na_tag(libcom) == "d" ~ 1),
    
    mil1  = case_when(spkmil == 1 ~ 0,
                      spkmil == 2 ~ 1,
                      is_tagged_na(spkmil) & na_tag(spkmil) == "d" ~ 1),
    mil2  = case_when(colmil == 4 ~ 0,
                      colmil == 5 ~ 1,
                      is_tagged_na(colmil) & na_tag(colmil) == "d" ~ 1),
    mil3  = case_when(libmil == 2 ~ 0,
                      libmil == 1 ~ 1,
                      is_tagged_na(libmil) & na_tag(libmil) == "d" ~ 1),
    
    hom1 = case_when(spkhomo == 1 ~ 0,
                     spkhomo == 2 ~ 1,
                     is_tagged_na(spkhomo) & na_tag(spkhomo) == "d" ~ 1),
    hom2 = case_when(colhomo == 4 ~ 0,
                     colhomo == 5 ~ 1,
                     is_tagged_na(colhomo) & na_tag(colhomo) == "d" ~ 1),
    hom3 = case_when(libhomo == 2 ~ 0,
                     libhomo == 1 ~ 1,
                     is_tagged_na(libhomo) & na_tag(libhomo) == "d" ~ 1)
  )

#Generate our variables, like before
gss_sub$stouffer <- gss_sub$ath1 + gss_sub$ath2 + gss_sub$ath3 + gss_sub$rac1 + gss_sub$rac2 + gss_sub$rac3 + gss_sub$com1 + gss_sub$com2 + gss_sub$com3 + gss_sub$mil1 + gss_sub$mil2 + gss_sub$mil3 + gss_sub$hom1 + gss_sub$hom2 + gss_sub$hom3

gss_sub$rac = gss_sub$rac1 + gss_sub$rac2 + gss_sub$rac3
gss_sub$hom = gss_sub$hom1 + gss_sub$hom2 + gss_sub$hom3

#Generate intolerance variables - keep 0 as 0; keep missing as missing, else; turn into 1
gss_sub <- gss_sub |>
  mutate(
    intol = case_when(
      stouffer == 0 ~ 0,
      stouffer %in% 1:15 ~ 1  # everything else
    )
  )

#Look at our results
table(gss_sub$stouffer)
table(gss_sub$rac)
table(gss_sub$hom)
table(gss_sub$intol)

gss_collapse <- gss_sub |>
  group_by(year) |>
  summarize(
    stouffer = mean(stouffer, na.rm = TRUE),
    rac = mean(rac, na.rm=T),
    hom = mean(hom, na.rm=T),
    intol = mean(intol, na.rm=T)
  )

table(gss_collapse$stouffer)
table(gss_collapse$rac)
table(gss_collapse$hom)
table(gss_collapse$intol)


plot(gss_collapse$year, gss_collapse$stouffer)
plot(gss_collapse$year, gss_collapse$rac)
plot(gss_collapse$year, gss_collapse$hom)
plot(gss_collapse$year, gss_collapse$intol)

#OK! We've made it to our first shot at the graphs.  Now it's time to correct some issues:

#Figure 1
#Make it nicer looking

plot(gss_collapse$year, gss_collapse$stouffer, type = "o", col = "blue",
     xlab = "Year", ylab = "Stouffer",
     main = "Mondak & Sanders Replication")

#Huh. There are some missing years
gss_collapse <- gss_collapse |>
  filter(!year %in% c(1978, 1983, 1986))

#Let's do it a little bit nicer
plot(gss_collapse$year, gss_collapse$stouffer, type = "l", col = "blue",
     xlab = "Year", ylab = "Stouffer",
     xlim = c(1976, 2021), ylim = c(0, 15),
     main = "Figure 1. The 0-15 GSS Modified Stouffer Tolerance Battery, 1976-2021")

#Let's do it properly nicer, with layering in base R

plot(x=0, type='n',
     xlim=c(1976, 2021), xaxt = "n",
     ylim=c(0,15), yaxt = "n",
     xlab="Year", 
     ylab="Stouffer", 
     main="Figure 1. The 0-15 GSS Modified Stouffer Tolerance Battery, 1976-2021")

axis(1, at = c(1976, 2021)) #1 says "bottom axis"
axis(2, at = c(0, 15)) #2 says "left axis"

lines(gss_collapse$year, gss_collapse$stouffer, lwd=2)
#points(gss_collapse$year, gss_collapse$stouffer)

#Let's put those arrows on anyways!
#use to check positioning
#text(1985, 8, "I want it that way", col = "firebrick3")


#Actual
#text(1985, 8, "I want it that way", col = "firebrick3")
#text(1990, 3.5, "I want it that way", col = "firebrick3")
#text(2010, 2.5, "I want it that way", col = "firebrick3")

#This is kind of a clunky procedure anwyays. So I'm not going to worry about doing this in an internal, "pull the nubmer from here" way
#I'm just going to look it up and put it in

print(gss_collapse, n=30)

#1977 is 6.84!
#1998 is 4.61!
#2021 is 4.41!

text(1985, 8, "7.24 (1977)")
text(1990, 3.5, "5.28 (1998)")
text(2014, 3.1, "4.43 (2021)")

#Let's get those arrows on

#Practice
#arrows(x0 = 1985, y0=9, x1=1984, y1=10,
#       length = 0.1,  # length of arrowhead
#       angle  = 30,   # angle of arrowhead
#       code   = 2,    # 1 = head at x0,y0; 2 = head at x1,y1; 3 = both
#       col    = "red", lwd = 2)

arrows(x0 = 1981.5, y0=7.8, x1=1977.5, y1=7.4,
       length = 0.1,  # length of arrowhead
       angle  = 30,   # angle of arrowhead
       code   = 2,    # 1 = head at x0,y0; 2 = head at x1,y1; 3 = both
       col    = "black", lwd = 1)

arrows(x0 = 1993.5, y0=3.7, x1=1997.5, y1=5,
       length = 0.1,  # length of arrowhead
       angle  = 30,   # angle of arrowhead
       code   = 2,    # 1 = head at x0,y0; 2 = head at x1,y1; 3 = both
       col    = "black", lwd = 1)

arrows(x0 = 2017, y0=3.5, x1=2020.5, y1=4.3,
       length = 0.1,  # length of arrowhead
       angle  = 30,   # angle of arrowhead
       code   = 2,    # 1 = head at x0,y0; 2 = head at x1,y1; 3 = both
       col    = "black", lwd = 1)

#Haha! 

#Figure 2
plot(gss_collapse$year, gss_collapse$rac, type = "l", col = "blue",
     xlab = "Year", ylab = "Stouffer",
     xlim = c(1976, 2021), ylim = c(0, 3),
     main = "Figure 2. Intolerance Toward Racists and Homosexuals, 1976-2021")

plot(gss_collapse$year, gss_collapse$hom, type = "l", col = "blue",
     xlab = "Year", ylab = "Stouffer",
     xlim = c(1976, 2021), ylim = c(0, 3),
     main = "Figure 2. Intolerance Toward Racists and Homosexuals, 1976-2021")

#Layer time
plot(x=0, type='n',
     xlim=c(1976, 2021), xaxt = "n",
     ylim=c(0,3), yaxt = "n",
     xlab=" ", 
     ylab=" ", 
     main="Figure 2. Intolerance Toward Racists and Homosexuals, 1976-2021")

axis(1, at = c(1976, 2021)) #1 says "bottom axis"
axis(2, at = c(0, 1, 2, 3)) #2 says "left axis"

lines(gss_collapse$year, gss_collapse$rac, lwd=2)
#lines(gss_collapse$year, gss_collapse$hom, lwd=2)
lines(gss_collapse$year, gss_collapse$hom, lwd=2, lty=2)

#Oh no, now all the text

text(1980, 1.5, "Racists")
text(1989, .7, "Homosexuals")


#Figure 5
plot(gss_collapse$year, gss_collapse$intol, type = "l", col = "blue",
     xlab = "Year", ylab = "Stouffer",
     xlim = c(1976, 2021), ylim = c(0, 1),
     main = "Figure 5. A Dichotomous Measure of Tolerance and Intolerance, 1976-2021")

#Layer time
plot(x=0, type='n',
     xlim=c(1976, 2021), xaxt = "n",
     ylim=c(0,1), yaxt = "n",
     xlab=" ", 
     ylab=" ", 
     main="Figure 5. A Dichotomous Measure of Tolerance and Intolerance, 1976-2021")

axis(1, at = c(1976, 2021)) #1 says "bottom axis"
axis(2, at = seq(0, 1, by = 0.1)) #2 says "left axis"

lines(gss_collapse$year, gss_collapse$intol, lwd=2)


##Let's do it in GGPlot

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

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 = "7.24 (1977)") +
  annotate("text", x = 1990, y = 3.5,  label = "5.28 (1998)") +
  annotate("text", x = 2014, y = 3.1,  label = "4.43 (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("figure1.pdf", 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("figure2.pdf", 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("figure5.pdf", fig5, width = 7.5, height = 7.55, dpi = 300)

#how about lattice?
library(lattice)

xyplot(
  stouffer ~ year,
  data = gss_collapse,
  type = "l",
  lwd = 2,
  xlab = "Year",
  ylab = "Stouffer",
  main = "Figure 1. The 0-15 GSS Modified Stouffer Tolerance Battery, 1976-2021",
  scales = list(
    x = list(limits = c(1976, 2021), at = c(1976, 2021)),
    y = list(limits = c(0, 15),      at = c(0, 15))
  ),
  panel = function(x, y, ...) {
    # line
    panel.xyplot(x, y, ...)
    
    # text labels
    panel.text(1985, 8.0,  "7.24 (1977)")
    panel.text(1990, 3.5,  "5.28 (1998)")
    panel.text(2014, 3.1,  "4.43 (2021)")
    
    # arrows (arrowhead at end, like base code=2)
    panel.arrows(
      x0 = 1981.5, y0 = 7.8, x1 = 1977.5, y1 = 7.4,
      length = unit(0.1, "in"), angle = 30, ends = "last",
      col = "black", lwd = 1
    )
    panel.arrows(
      x0 = 1993.5, y0 = 3.7, x1 = 1997.5, y1 = 5.0,
      length = unit(0.1, "in"), angle = 30, ends = "last",
      col = "black", lwd = 1
    )
    panel.arrows(
      x0 = 2017.0, y0 = 3.5, x1 = 2020.5, y1 = 4.3,
      length = unit(0.1, "in"), angle = 30, ends = "last",
      col = "black", lwd = 1
    )
  }
)


#Figure 2

xyplot(
  rac + hom ~ year,
  data = gss_collapse,
  type = "l",
  lwd = 1,
  lty = c(1, 2),  # solid for rac, dashed for hom
  scales = list(
    x = list(limits = c(1976, 2021), at = c(1976, 2021)),
    y = list(limits = c(0, 3), at = 0:3)
  ),
  xlab = "",
  ylab = "",
  main = "Figure 2. Intolerance Toward Racists and Homosexuals, 1976-2021",
  panel = function(x, y, ...) {
    panel.superpose(x, y, ...)  # draw the two series
    
    # add text labels directly
    panel.text(1980, 1.5, "Racists")
    panel.text(1989, 0.7, "Homosexuals")
  }
)

#Figure 5 

xyplot(
  intol ~ year,
  data = gss_collapse,
  type = "l",
  col = "blue",
  lwd = 2,
  xlab = "Year",
  ylab = "Stouffer",
  main = "Figure 5. A Dichotomous Measure of Tolerance and Intolerance, 1976-2021",
  scales = list(
    x = list(limits = c(1976, 2021), at = c(1976, 2021)),
    y = list(limits = c(0, 1), at = seq(0, 1, by = 0.1))
  ),
  panel = function(x, y, ...) {
    panel.xyplot(x, y, ...)
  }
)
