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

bartels1B<-ggplot(figure1_data, aes(x = year)) +
  #Next, our scales and axes
  scale_y_continuous(
    limits = c(0, 0.5),
    breaks = seq(0, 0.5, 0.1),
    minor_breaks = seq(0, 0.5, 0.05),
    name = NULL
  ) +
  scale_x_continuous(
    breaks = seq(1956, endyear, 8),
    minor_breaks = seq(1952, endyear, 4),
    name = "Year"
  ) +
  # Reference lines
  # Horizontal reference lines at 0.1 ... 0.5 (vvthin + dotted + black)
  geom_hline(yintercept = seq(0.1, 0.5, 0.1),
             linetype = "dotted", linewidth = 0.2, color = "black") +
  # Vertical reference lines at election years (vvthin + dotted + black)
  geom_vline(
    xintercept = seq(1956, endyear, by = 8),
    linetype = "dotted",
    linewidth = 0.2,
    color = "black"
  ) +
  
  
  # Leaners: dashed + hollow
  geom_line(aes(y = leaners, linetype = "Independent Leaners"), linewidth = 0.7, color = "black") +
  geom_point(aes(y = leaners, shape = "Independent Leaners"),
             size = 2.2, stroke = 0.9, color = "black", fill = NA) +
  
  # Pure Independents: solid + filled
  geom_line(aes(y = indeps, linetype = "Pure Independents"), linewidth = 0.7, color = "black") +
  geom_point(aes(y = indeps, shape = "Pure Independents"),
             size = 2.2, stroke = 0.8, color = "black", fill = "black") +
  
  
  labs(
    title = paste0("Distribution of Party Identification 1952-", endyear), #Paste puts a string in as specified, which lets me use my passed endyear variable
    subtitle = "Proportions of National Election Study Sample",
    caption = caption_text
  ) +
  
  # Map linetypes and shapes to series (order matches your legend order)
  scale_linetype_manual(
    values = c("Independent Leaners" = "dashed",
               "Pure Independents"   = "solid"),
    guide = guide_legend(order = 1)
  ) +
  scale_shape_manual(
    values = c("Independent Leaners" = 1,   # hollow circle
               "Pure Independents"   = 16), # filled circle
    guide = guide_legend(order = 1)
  ) +
  
  # Legend: one column, inside the plot (similar to position(5) ring(0))
  guides(
    linetype = guide_legend(ncol = 1, title = NULL),
    shape    = guide_legend(ncol = 1, title = NULL)
  ) +
  theme_minimal(base_size = 12) +
  theme(
    panel.border = element_rect(color = "black", fill = NA, linewidth = 0.5),
    legend.position = c(0.4, 0.8),  # tweak as desired
    legend.background = element_rect(fill = "white", color = "grey70", linewidth = 0.3), # light box
    panel.grid.minor = element_line(linewidth = 0.2),
    panel.grid.major = element_line(linewidth = 0.3),
    plot.title = element_text(face = "bold") 
  ) 
bartels1B
