library(ggplot2)
library(tidyr)

# ── LITERACY ──────────────────────────────────────────────────────────────────

lit_data <- data.frame(
  Cycle     = factor(c("BL", "ML", "EL"), levels = c("BL", "ML", "EL")),
  Beginner  = c(53, 34, 20),
  Letter    = c(17, 23, 21),
  Word      = c(16, 20, 24),
  Paragraph = c(8,  12, 17),
  Story     = c(5,  10, 18)
)

long_lit   <- pivot_longer(lit_data, cols = -Cycle, names_to = "Level", values_to = "Percent")
lit_order  <- c("Beginner", "Letter", "Word", "Paragraph", "Story")
long_lit$Level <- factor(long_lit$Level, levels = rev(lit_order))

lit_colors <- c(
  "Beginner"  = "#fff5f0", "Letter" = "#fee0d2",
  "Word"      = "#fcbba1", "Paragraph" = "#fc9272", "Story" = "#cb181d"
)

rect_lit <- data.frame(
  xmin = c(0.73, 1.73, 2.73), xmax = c(1.27, 2.27, 3.27),
  ymin = c(84,   75,   63),   ymax = c(101,  101,  102)
)

ggplot(long_lit, aes(x = Cycle, y = Percent, fill = Level)) +
  geom_bar(stat = "identity", width = 0.45) +
  geom_text(aes(label = paste0(Percent, "%")),
            position = position_stack(vjust = 0.5), size = 4) +
  annotate("rect", xmin = rect_lit$xmin, xmax = rect_lit$xmax,
           ymin = rect_lit$ymin, ymax = rect_lit$ymax,
           fill = NA, color = "black", linetype = "dotted", linewidth = 1) +
  scale_fill_manual(values = lit_colors, breaks = lit_order) +
  scale_y_continuous(limits = c(0, 106), breaks = c(0, 25, 50, 75, 100),
                     labels = function(x) paste0(x, "%")) +
  labs(title    = "North Western Literacy Outcome",
       subtitle = "Learners reading at Paragraph and Story level more than doubled,\nfrom 13% at Baseline to 35% at Endline",
       x = "", y = "") +
  theme_minimal(base_size = 14) +
  theme(plot.title = element_text(hjust = 0.5, face = "bold"),
        plot.subtitle = element_text(hjust = 0.5),
        legend.title = element_blank(), panel.grid.major.x = element_blank())

# ── NUMERACY ──────────────────────────────────────────────────────────────────

math_data <- data.frame(
  Cycle           = factor(c("BL", "ML", "EL"), levels = c("BL", "ML", "EL")),
  `Beginner Math` = c(8,  3,  2),
  `1-digit`       = c(21, 10, 4),
  `2-digit`       = c(34, 25, 15),
  `3-digit`       = c(11, 15, 15),
  `4-digit`       = c(3,  9,  15),
  `Can add`       = c(10, 14, 12),
  `Can subtract`  = c(7,  13, 18),
  `Can mult`      = c(5,  9,  12),
  `Can divide`    = c(2,  3,  8),
  check.names     = FALSE
)

long_math  <- pivot_longer(math_data, cols = -Cycle, names_to = "Level", values_to = "Percent")
num_order  <- c("Beginner Math", "1-digit", "2-digit", "3-digit", "4-digit",
                "Can add", "Can subtract", "Can mult", "Can divide")
long_math$Level <- factor(long_math$Level, levels = rev(num_order))

num_colors <- c(
  "Beginner Math" = "#f7fbff", "1-digit" = "#deebf7", "2-digit" = "#c6dbef",
  "3-digit"       = "#9ecae1", "4-digit" = "#6baed6", "Can add" = "#74c476",
  "Can subtract"  = "#41ab5d", "Can mult" = "#238b45", "Can divide" = "#005a32"
)

# Circles around Can subtract + Can mult + Can divide
# BL: Can subtract starts at 8+21+34+11+3+10=87, Can divide ends at 99
# ML: Can subtract starts at 3+10+25+15+9+14=76, Can divide ends at 101
# EL: Can subtract starts at 2+4+15+15+15+12=63, Can divide ends at 101
rect_math <- data.frame(
  xmin = c(0.73, 1.73, 2.73), xmax = c(1.27, 2.27, 3.27),
  ymin = c(85,   74,   61),   ymax = c(101,  102,  102)
)

ggplot(long_math, aes(x = Cycle, y = Percent, fill = Level)) +
  geom_bar(stat = "identity", width = 0.45) +
  geom_text(aes(label = paste0(Percent, "%")),
            position = position_stack(vjust = 0.5), size = 3.5) +
  annotate("rect", xmin = rect_math$xmin, xmax = rect_math$xmax,
           ymin = rect_math$ymin, ymax = rect_math$ymax,
           fill = NA, color = "black", linetype = "dotted", linewidth = 1) +
  scale_fill_manual(values = num_colors, breaks = num_order) +
  scale_y_continuous(limits = c(0, 106), breaks = c(0, 25, 50, 75, 100),
                     labels = function(x) paste0(x, "%")) +
  labs(title    = "North Western Numeracy Outcome",
       subtitle = "Learners who can subtract, multiply and divide nearly tripled,\nfrom 14% at Baseline to 38% at Endline",
       x = "", y = "") +
  theme_minimal(base_size = 14) +
  theme(plot.title = element_text(hjust = 0.5, face = "bold"),
        plot.subtitle = element_text(hjust = 0.5),
        legend.title = element_blank(), panel.grid.major.x = element_blank())

library(ggplot2)
library(tidyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# ── LITERACY: Readers vs Non-Readers ─────────────────────────────────────────
# Readers      = Paragraph + Story
# Non-Readers  = Beginner + Letter + Word

lit_binary <- data.frame(
  Cycle        = factor(c("BL", "ML", "EL"), levels = c("BL", "ML", "EL")),
  Readers      = c(8+5, 12+10, 17+18),       # 13, 22, 35
  `Non-Readers` = c(100-13, 100-22, 100-35), # 87, 78, 65
  check.names  = FALSE
)

long_lit2 <- pivot_longer(lit_binary, cols = -Cycle,
                           names_to = "Group", values_to = "Percent")

# Non-Readers at bottom, Readers on top
long_lit2$Group <- factor(long_lit2$Group, levels = c("Readers", "Non-Readers"))

ggplot(long_lit2, aes(x = Cycle, y = Percent, fill = Group)) +
  geom_bar(stat = "identity", width = 0.45) +
  geom_text(aes(label = paste0(Percent, "%")),
            position = position_stack(vjust = 0.5), size = 5, fontface = "bold") +
  scale_fill_manual(values = c("Readers" = "#cb181d", "Non-Readers" = "#fcbba1")) +
  scale_y_continuous(limits = c(0, 100),
                     breaks = c(0, 25, 50, 75, 100),
                     labels = function(x) paste0(x, "%")) +
  labs(title    = "North Western Literacy Outcome",
       subtitle = "Share of Readers (Paragraph + Story) grew from 13% at Baseline to 35% at Endline",
       x = "", y = "") +
  theme_minimal(base_size = 14) +
  theme(plot.title         = element_text(hjust = 0.5, face = "bold"),
        plot.subtitle      = element_text(hjust = 0.5),
        legend.title       = element_blank(),
        panel.grid.major.x = element_blank())

# ── NUMERACY: Subtractors vs Non-Subtractors ──────────────────────────────────
# Subtractors     = Can subtract + Can mult + Can divide
# Non-Subtractors = the rest

num_binary <- data.frame(
  Cycle             = factor(c("BL", "ML", "EL"), levels = c("BL", "ML", "EL")),
  Subtractors       = c(7+5+2, 13+9+3, 18+12+8),      # 14, 25, 38
  `Non-Subtractors` = c(100-14, 100-25, 100-38),       # 86, 75, 62
  check.names       = FALSE
)

long_num2 <- pivot_longer(num_binary, cols = -Cycle,
                           names_to = "Group", values_to = "Percent")

long_num2$Group <- factor(long_num2$Group, levels = c("Subtractors", "Non-Subtractors"))

ggplot(long_num2, aes(x = Cycle, y = Percent, fill = Group)) +
  geom_bar(stat = "identity", width = 0.45) +
  geom_text(aes(label = paste0(Percent, "%")),
            position = position_stack(vjust = 0.5), size = 5, fontface = "bold") +
  scale_fill_manual(values = c("Subtractors" = "#238b45", "Non-Subtractors" = "#c6dbef")) +
  scale_y_continuous(limits = c(0, 100),
                     breaks = c(0, 25, 50, 75, 100),
                     labels = function(x) paste0(x, "%")) +
  labs(title    = "North Western Numeracy Outcome",
       subtitle = "Share of Subtractors grew from 14% at Baseline to 38% at Endline",
       x = "", y = "") +
  theme_minimal(base_size = 14) +
  theme(plot.title         = element_text(hjust = 0.5, face = "bold"),
        plot.subtitle      = element_text(hjust = 0.5),
        legend.title       = element_blank(),
        panel.grid.major.x = element_blank())