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

#You can build a GGPlot in any order you like
#Generally, it's advisable to follow similar patterns
#I like to build "up" from the base plot area, finishing with styling
#1. Plot Area
#2. Scales & Axes
#3. Main Data
#4. Labels/Titles
#5. Styling

bartels1A <- 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"
  ) +
  
  
  # Strong: connected (solid line + filled points)
  
  geom_line(aes(y = strong, linetype = "Strong Identifiers"), linewidth = 0.7, color = "black") +
  geom_point(aes(y = strong, shape = "Strong Identifiers"), size = 2.2, stroke = 0.8,
             color = "black", fill = "black") +
  
  # Weak: connected (dashed line + hollow circle points)
  geom_line(aes(y = weak, linetype = "Weak Identifiers"), linewidth = 0.7, color = "black") +
  geom_point(aes(y = weak, shape = "Weak Identifiers"), size = 2.2, stroke = 0.9,
             color = "black", fill = NA) +
  
  #Titles and labeling
  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 legend order)
  scale_linetype_manual(
    values = c("Strong Identifiers" = "solid",
               "Weak Identifiers"   = "dashed"),
    guide = guide_legend(order = 1)
  ) +
  scale_shape_manual(
    values = c("Strong Identifiers" = 16,   # filled circle
               "Weak Identifiers"   = 1),   # hollow circle
    guide = guide_legend(order = 1)
  ) +
  
  # Legend: one column, inside the plot
  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.6, 0.2),  # 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") 
  ) 
bartels1A
