|
| 1 | +# ============================================================================= |
| 2 | +# Create hex sticker for cfperformance |
| 3 | +# ============================================================================= |
| 4 | + |
| 5 | +# Install hexSticker if needed |
| 6 | +# install.packages("hexSticker") |
| 7 | +# install.packages("showtext") |
| 8 | + |
| 9 | +library(hexSticker) |
| 10 | +library(ggplot2) |
| 11 | +library(showtext) |
| 12 | + |
| 13 | +# Enable showtext for better fonts |
| 14 | +font_add_google("Fira Sans", "firasans") |
| 15 | +showtext_auto() |
| 16 | + |
| 17 | +# ----------------------------------------------------------------------------- |
| 18 | +# Design 1: ROC Curve with Counterfactual Split |
| 19 | +# Shows an ROC curve splitting into two paths (treatment vs control) |
| 20 | +# ----------------------------------------------------------------------------- |
| 21 | + |
| 22 | +create_roc_design <- function() { |
| 23 | + # Create data for the curves |
| 24 | + x <- seq(0, 1, length.out = 100) |
| 25 | + |
| 26 | + # Main ROC curve (observed) |
| 27 | + roc_main <- data.frame( |
| 28 | + x = x, |
| 29 | + y = x^0.35, |
| 30 | + group = "main" |
| 31 | + ) |
| 32 | + |
| 33 | + # Counterfactual ROC curve (what could have been) |
| 34 | + roc_cf <- data.frame( |
| 35 | + x = x, |
| 36 | + y = x^0.5, |
| 37 | + group = "cf" |
| 38 | + ) |
| 39 | + |
| 40 | + p <- ggplot() + |
| 41 | + # Reference diagonal |
| 42 | + geom_segment(aes(x = 0, y = 0, xend = 1, yend = 1), |
| 43 | + linetype = "dashed", color = "gray60", linewidth = 0.5) + |
| 44 | + # Counterfactual ROC (dashed, representing what we estimate) |
| 45 | + geom_line(data = roc_cf, aes(x = x, y = y), |
| 46 | + color = "#7ECCE8", linewidth = 1.2, linetype = "dashed") + |
| 47 | + # Observed ROC |
| 48 | + geom_line(data = roc_main, aes(x = x, y = y), |
| 49 | + color = "#FFFFFF", linewidth = 1.5) + |
| 50 | + # Add small arrows to suggest causality |
| 51 | + annotate("segment", x = 0.5, y = 0.5^0.35, xend = 0.5, yend = 0.5^0.5, |
| 52 | + arrow = arrow(length = unit(0.1, "cm"), type = "closed"), |
| 53 | + color = "#F4D35E", linewidth = 0.8) + |
| 54 | + coord_fixed(ratio = 1) + |
| 55 | + xlim(0, 1) + ylim(0, 1) + |
| 56 | + theme_void() + |
| 57 | + theme(legend.position = "none") |
| 58 | + |
| 59 | + return(p) |
| 60 | +} |
| 61 | + |
| 62 | +# ----------------------------------------------------------------------------- |
| 63 | +# Design 2: Stylized "cf" with Performance Curve |
| 64 | +# ----------------------------------------------------------------------------- |
| 65 | + |
| 66 | +create_cf_design <- function() { |
| 67 | + # Create an elegant curve representing performance |
| 68 | + x <- seq(0, 1, length.out = 100) |
| 69 | + |
| 70 | + p <- ggplot() + |
| 71 | + # Calibration-style curve |
| 72 | + geom_segment(aes(x = 0, y = 0, xend = 1, yend = 1), |
| 73 | + linetype = "dashed", color = "gray50", linewidth = 0.4) + |
| 74 | + # Performance curve |
| 75 | + stat_function(fun = function(x) pnorm(qnorm(x) + 0.5), |
| 76 | + color = "#FFFFFF", linewidth = 1.3, n = 200) + |
| 77 | + # Second curve for counterfactual |
| 78 | + stat_function(fun = function(x) pnorm(qnorm(x) + 1.0), |
| 79 | + color = "#7ECCE8", linewidth = 1.0, linetype = "dashed", n = 200) + |
| 80 | + coord_fixed(ratio = 1) + |
| 81 | + xlim(0, 1) + ylim(0, 1) + |
| 82 | + theme_void() |
| 83 | + |
| 84 | + return(p) |
| 85 | +} |
| 86 | + |
| 87 | +# ----------------------------------------------------------------------------- |
| 88 | +# Design 3: Abstract Causal Paths |
| 89 | +# Two diverging paths representing treatment/control outcomes |
| 90 | +# ----------------------------------------------------------------------------- |
| 91 | + |
| 92 | +create_causal_design <- function() { |
| 93 | + # Create diverging paths |
| 94 | + t <- seq(0, 1, length.out = 50) |
| 95 | + |
| 96 | + # Path 1 (treatment outcome trajectory) |
| 97 | + path1 <- data.frame( |
| 98 | + x = t, |
| 99 | + y = 0.5 + 0.3 * sin(t * pi) + 0.15 * t, |
| 100 | + group = "treated" |
| 101 | + ) |
| 102 | + |
| 103 | + # Path 2 (control outcome trajectory) |
| 104 | + path2 <- data.frame( |
| 105 | + x = t, |
| 106 | + y = 0.5 + 0.15 * sin(t * pi) - 0.1 * t, |
| 107 | + group = "control" |
| 108 | + ) |
| 109 | + |
| 110 | + # Starting point |
| 111 | + start_point <- data.frame(x = 0, y = 0.5) |
| 112 | + |
| 113 | + p <- ggplot() + |
| 114 | + geom_line(data = path1, aes(x = x, y = y), |
| 115 | + color = "#FFFFFF", linewidth = 1.5) + |
| 116 | + geom_line(data = path2, aes(x = x, y = y), |
| 117 | + color = "#7ECCE8", linewidth = 1.2, linetype = "longdash") + |
| 118 | + geom_point(data = start_point, aes(x = x, y = y), |
| 119 | + color = "#F4D35E", size = 3) + |
| 120 | + xlim(-0.1, 1.1) + ylim(0, 1) + |
| 121 | + theme_void() |
| 122 | + |
| 123 | + return(p) |
| 124 | +} |
| 125 | + |
| 126 | +# ----------------------------------------------------------------------------- |
| 127 | +# Create the hex stickers |
| 128 | +# ----------------------------------------------------------------------------- |
| 129 | + |
| 130 | +# Color palette (medical/clinical blues) |
| 131 | +bg_color <- "#1A365D" # Deep navy |
| 132 | +border_color <- "#2E86AB" # Bright blue |
| 133 | +accent_color <- "#F4D35E" # Gold accent |
| 134 | + |
| 135 | +# Design 1: ROC curve (recommended) |
| 136 | +p1 <- create_roc_design() |
| 137 | + |
| 138 | +sticker(p1, |
| 139 | + package = "cfperformance", |
| 140 | + p_size = 16, |
| 141 | + p_y = 1.45, |
| 142 | + p_color = "#FFFFFF", |
| 143 | + p_family = "firasans", |
| 144 | + s_x = 1, |
| 145 | + s_y = 0.75, |
| 146 | + s_width = 1.4, |
| 147 | + s_height = 1.1, |
| 148 | + h_fill = bg_color, |
| 149 | + h_color = border_color, |
| 150 | + h_size = 1.5, |
| 151 | + url = "github.com/boyercb/cfperformance", |
| 152 | + u_size = 3.5, |
| 153 | + u_color = "#7ECCE8", |
| 154 | + filename = "inst/hex/cfperformance_hex_roc.png", |
| 155 | + dpi = 300) |
| 156 | + |
| 157 | +# Design 2: Calibration/performance curve |
| 158 | +p2 <- create_cf_design() |
| 159 | + |
| 160 | +sticker(p2, |
| 161 | + package = "cfperformance", |
| 162 | + p_size = 16, |
| 163 | + p_y = 1.45, |
| 164 | + p_color = "#FFFFFF", |
| 165 | + p_family = "firasans", |
| 166 | + s_x = 1, |
| 167 | + s_y = 0.75, |
| 168 | + s_width = 1.4, |
| 169 | + s_height = 1.1, |
| 170 | + h_fill = bg_color, |
| 171 | + h_color = border_color, |
| 172 | + h_size = 1.5, |
| 173 | + url = "github.com/boyercb/cfperformance", |
| 174 | + u_size = 3.5, |
| 175 | + u_color = "#7ECCE8", |
| 176 | + filename = "inst/hex/cfperformance_hex_calib.png", |
| 177 | + dpi = 300) |
| 178 | + |
| 179 | +# Design 3: Causal paths |
| 180 | +p3 <- create_causal_design() |
| 181 | + |
| 182 | +sticker(p3, |
| 183 | + package = "cfperformance", |
| 184 | + p_size = 16, |
| 185 | + p_y = 1.45, |
| 186 | + p_color = "#FFFFFF", |
| 187 | + p_family = "firasans", |
| 188 | + s_x = 1, |
| 189 | + s_y = 0.75, |
| 190 | + s_width = 1.4, |
| 191 | + s_height = 1.1, |
| 192 | + h_fill = bg_color, |
| 193 | + h_color = border_color, |
| 194 | + h_size = 1.5, |
| 195 | + url = "github.com/boyercb/cfperformance", |
| 196 | + u_size = 3.5, |
| 197 | + u_color = "#7ECCE8", |
| 198 | + filename = "inst/hex/cfperformance_hex_causal.png", |
| 199 | + dpi = 300) |
| 200 | + |
| 201 | +# ----------------------------------------------------------------------------- |
| 202 | +# Print summary |
| 203 | +# ----------------------------------------------------------------------------- |
| 204 | + |
| 205 | +cat("\n") |
| 206 | +cat("==============================================\n") |
| 207 | +cat("Hex stickers created in inst/hex/\n") |
| 208 | +cat("==============================================\n") |
| 209 | +cat("\n") |
| 210 | +cat("Files:\n") |
| 211 | +cat(" - cfperformance_hex_roc.png (ROC curve design)\n") |
| 212 | +cat(" - cfperformance_hex_calib.png (Calibration curve design)\n") |
| 213 | +cat(" - cfperformance_hex_causal.png (Causal paths design)\n") |
| 214 | +cat("\n") |
| 215 | +cat("To use in README, add to man/figures/ and reference:\n") |
| 216 | +cat(' <img src="man/figures/cfperformance_hex.png" align="right" height="139" />\n') |
| 217 | +cat("\n") |
0 commit comments