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

library(lattice)
library(grid)

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

fig<-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, paste(round(v77, 2), "(1977)"))
    panel.text(1990, 3.5, paste(round(v98, 2), "(1998)"))
    panel.text(2014, 3.1, paste(round(v21, 2), "(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
    )
  }
)

fig

png("figures/lattice/fig1_lattice.png", width = 8, height = 5, units = "in", res = 300)
print(fig)
dev.off()

pdf("figures/lattice/fig1_lattice.pdf", width = 8, height = 5)
print(fig)
dev.off()

#Figure 2

fig<-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")
  }
)

fig

png("figures/lattice/fig2_lattice.png", width = 8, height = 5, units = "in", res = 300)
print(fig)
dev.off()

pdf("figures/lattice/fig2_lattice.pdf", width = 8, height = 5)
print(fig)
dev.off()

#Figure 5 

fig<-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, ...)
  }
)

fig

png("figures/lattice/fig5_lattice.png", width = 8, height = 5, units = "in", res = 300)
print(fig)
dev.off()

pdf("figures/lattice/fig5_lattice.pdf", width = 8, height = 5)
print(fig)
dev.off()