#Title: DWV Assign 7 - Bartels (Figure 2).R
#Author: Jack Reilly
#Purpose: Draw Figure 2 from Bartels
#Requires: Managed objects from prior scripts
#Output: Figures as PDFs (ggplot only)

bartels2<-ggplot() +
  # horizontal y reference lines at .1(.1)1
  geom_hline(yintercept = seq(0.1, 1.0, 0.1),
             linetype = "dotted", linewidth = 0.2, color = "black") +
  # vertical x reference lines at listed years
  geom_vline(xintercept = seq(1952, endyear, by = 4),
             linetype = "dotted", linewidth = 0.2, color = "black") +
  
  # nonvoters: dashed line + hollow circles
  geom_line(data = subset(figure2_data, turnout == 0),
            aes(x = year, y = identifiers, linetype = "Nonvoters"),
            linewidth = 0.7, color = "black") +
  geom_point(data = subset(figure2_data, turnout == 0),
             aes(x = year, y = identifiers, shape = "Nonvoters"),
             size = 2.2, stroke = 0.9, color = "black", fill = NA) +
  
  # voters: solid line + filled circles
  geom_line(data = subset(figure2_data, turnout == 1),
            aes(x = year, y = identifiers, linetype = "Voters"),
            linewidth = 0.7, color = "black") +
  geom_point(data = subset(figure2_data, turnout == 1),
             aes(x = year, y = identifiers, shape = "Voters"),
             size = 2.2, stroke = 0.8, color = "black", fill = "black") +
  
  # axes, labels, scales
  scale_y_continuous(
    limits = c(0, 1),
    breaks = seq(0, 1, 0.1),
    minor_breaks = seq(0, 1, 0.05),
    name = NULL  # ytitle("")
  ) +
  scale_x_continuous(
    breaks = seq(1956, endyear, 8),
    minor_breaks = seq(1956, endyear, 4),
    name = "Year"  # xtitle
  ) +
  
  # title/subtitle/note
  labs(
    title = "Party Identification Among Presidential Voters and Nonvoters",
    subtitle = "Proportions of Strong or Weak Identfiers in National Election Study Sample",
    caption = "Presidential Election Years Only Weighted",
    linetype = NULL, shape = NULL
  ) +
  
  # legend styling & ordering
  scale_linetype_manual(values = c("Nonvoters" = "dashed", "Voters" = "solid")) +
  scale_shape_manual(values = c("Nonvoters" = 1, "Voters" = 16)) +
  guides(
    linetype = guide_legend(ncol = 1, order = 1),
    shape    = guide_legend(ncol = 1, order = 1)
  ) +
  
  # theme tweaks (panel box + light legend box, legend inside near top-right)
  theme_minimal(base_size = 12) +
  theme(
    panel.border     = element_rect(color = "black", fill = NA, linewidth = 0.5),
    panel.grid.minor = element_line(linewidth = 0.2),
    panel.grid.major = element_line(linewidth = 0.3),
    legend.position  = c(0.2, 0.2),  # tweak
    legend.background= element_rect(fill = "white", color = "grey70", linewidth = 0.3),
    plot.title       = element_text(face = "bold")
  )

bartels2