## Multiview hex sticker v2 -- Stanford palette, asymmetric matrices, with y.
##
##   [X (5 cols)]   +   [Z (3 cols)]   ~   [y (1 col)]
##
##   X: cardinal red gradient
##   Z: bronze/sandstone gradient
##   y: cool grey gradient (response vector)

library(hexSticker)
library(ggplot2)

set.seed(42)
NROW <- 5

build_block <- function(col_start, n_cols, palette, n_rows = NROW) {
  df <- expand.grid(col = col_start:(col_start + n_cols - 1), row = 1:n_rows)
  df$val <- runif(nrow(df), 0.30, 1.0)
  ramp <- colorRampPalette(palette)(100)
  df$fill <- ramp[round(df$val * 99) + 1]
  df
}

## X: cols 1:5 in cardinal red
xmat <- build_block(1, 5, c("#E0AAAA", "#8C1515"))
## Z: cols 7:9 in Stanford bronze
zmat <- build_block(7, 3, c("#D6CFA0", "#867440"))
## y: col 11 in cool grey
yvec <- build_block(11, 1, c("#C8C9CB", "#4D4F53"))

both <- rbind(xmat, zmat, yvec)

p <- ggplot(both, aes(x = col, y = row, fill = fill)) +
  geom_tile(color = "white", linewidth = 0.3) +
  scale_fill_identity() +
  ## agreement-penalty "+" between X and Z
  annotate("text", x = 6, y = 3, label = "+",
           size = 10, color = "#2e2d29", fontface = "bold") +
  ## "~" between (X,Z) and y -- the regression/prediction relation
  annotate("text", x = 10, y = 3, label = "~",
           size = 10, color = "#2e2d29", fontface = "bold") +
  ## Bold italic serif labels (standard stats notation)
  annotate("text", x = 3,  y = 6.2, label = "bolditalic(X)",
           parse = TRUE, size = 11, color = "#2e2d29",
           family = "serif") +
  annotate("text", x = 8,  y = 6.2, label = "bolditalic(Z)",
           parse = TRUE, size = 11, color = "#2e2d29",
           family = "serif") +
  annotate("text", x = 11, y = 6.2, label = "bolditalic(y)",
           parse = TRUE, size = 11, color = "#2e2d29",
           family = "serif") +
  coord_fixed(clip = "off", xlim = c(0.5, 11.5)) +
  theme_void() +
  theme(plot.background = element_blank(),
        panel.background = element_blank(),
        plot.margin = margin(0, 0, 0, 0))

dir.create("/tmp/hex_v2", showWarnings = FALSE)

sticker(
  subplot = p, s_x = 1, s_y = 1.08, s_width = 1.7, s_height = 0.95,
  package = "multiview", p_size = 22,
  p_color = "#8C1515", p_y = 0.50,
  h_fill = "#FFFFFF", h_color = "#8C1515", h_size = 1.4,
  filename = "/tmp/hex_v2/v2_stanford_with_y.png", dpi = 600
)

cat("done. /tmp/hex_v2/v2_stanford_with_y.png\n")
