File size: 3,880 Bytes
6c8d007
 
 
5cb8972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edefa8f
 
 
 
 
5cb8972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c8d007
 
 
 
 
 
0117dd6
 
 
 
 
6c8d007
0117dd6
6c8d007
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
library(cowplot)
library(ggplot2)

# =============================================================================
# CENTRALIZED COLOR PALETTE (WCAG AA Accessible)
# =============================================================================

#' Partisan color palette for consistent, accessible visualization
partisan_colors <- list(
  republican = "#D32F2F",      # Accessible red

  democrat = "#1976D2",        # Accessible blue
  independent = "#388E3C",     # Accessible green
  total = "#424242",           # Dark gray for totals
  reference = "#757575",       # Medium gray for reference lines
  cautious = "#7B1FA2"         # Purple for best-case/cautious model
)

#' Get named vector of partisan colors for ggplot scale_color_manual
#' @return Named character vector
get_partisan_colors <- function() {
  c(
    "Republican" = partisan_colors$republican,
    "Democrat" = partisan_colors$democrat,
    "Independent" = partisan_colors$independent,
    "Republicans" = partisan_colors$republican,
    "Democrats" = partisan_colors$democrat,
    "Independents" = partisan_colors$independent,
    "Total" = partisan_colors$total,
    "Republicans Infected" = partisan_colors$republican,
    "Democrats Infected" = partisan_colors$democrat,
    "Independents Infected" = partisan_colors$independent
  )
}

#' Get named vector for line types
#' @return Named character vector
get_model_linetypes <- function() {
  c(
    "Reference Model" = "dashed",
    "Current Model" = "solid",
    "Best Case Model" = "dotted",
    "Homogenous" = "dashed"
  )
}

# =============================================================================
# CONSISTENT PLOT THEME
# =============================================================================

#' Custom theme for partisan disease plots
#'
#' A clean, professional theme with subtle grid lines for readability
#'
#' @param base_size Base font size (default 14)
#' @param title_size Title font size (default 16)
#' @return ggplot2 theme object
theme_partisan <- function(base_size = 14, title_size = 16) {
  theme_cowplot(font_size = base_size) +
    theme(
      # Title and subtitle
      plot.title = element_text(
        size = title_size,
        face = "bold",
        color = "#424242",
        margin = margin(b = 8)
      ),
      plot.subtitle = element_text(
        size = base_size - 1,
        color = "#616161",
        margin = margin(b = 8),
        lineheight = 1.3
      ),

      # Axis styling
      axis.title = element_text(size = base_size - 1, color = "#424242"),
      axis.text = element_text(size = base_size - 2, color = "#616161"),
      axis.line = element_line(color = "#BDBDBD", size = 0.5),

      # Subtle grid for readability
      panel.grid.major.y = element_line(color = "#EEEEEE", size = 0.3),
      panel.grid.major.x = element_blank(),
      panel.grid.minor = element_blank(),

      # Legend styling
      legend.background = element_rect(fill = "white", color = NA),
      legend.title = element_text(size = base_size - 1, face = "bold"),
      legend.text = element_text(size = base_size - 2),

      # Overall plot styling
      plot.background = element_rect(fill = "white", color = NA),
      panel.background = element_rect(fill = "#FAFAFA", color = NA),

      # Margins
      plot.margin = margin(t = 10, r = 10, b = 10, l = 10)
    )
}

# =============================================================================
# UTILITY FUNCTIONS
# =============================================================================

#' Get legend from a ggplot object
#'
#' @param plot ggplot object
#' @param legend Optional legend identifier
#' @return Legend grob
get_legend_35 <- function(plot, legend = NULL) {
    # Use cowplot's get_legend as fallback
    tryCatch({
        leg <- cowplot::get_legend(plot)
        return(leg)
    }, error = function(e) {
        return(NULL)
    })
}