# ---------------------------------------------------------------------------
# make_figures.R — figure for this post (tenure-track jobs on LINGUIST List)
#
# Fig 1: tenure-track job posts by subfield and job-market season (Jul–Jun),
#        2016–17 to 2025–26 (counts).
# Fig 2: each subfield's share of that season's tenure-track posts.
#
# Run from this post's folder:  Rscript make_figures.R
#
# Input: jobs.csv, one row per LINGUIST List job post (built by the Python
# scripts in linguistlist-jobs-code.zip). A post counts as tenure-track when
# tenure_line == 1. A post that lists several subfields counts once in each, so
# shares within a season do not add up to 100%. Subfields with fewer than 10
# tenure-track posts in total are dropped.
# ---------------------------------------------------------------------------

library(ggplot2)

out_dir <- "figures"
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)

col_ink <- "#3a3a38"; col_muted <- "#75746e"
ramp    <- c("#f4f8fd", "#cde2fb", "#86b6ef", "#3987e5", "#1c5cab", "#0d366b")

base_theme <- theme_minimal(base_size = 12) +
  theme(panel.grid = element_blank(),
        axis.text  = element_text(color = col_ink),
        axis.title = element_blank(),
        plot.title = element_text(face = "bold", size = 13, color = col_ink),
        plot.subtitle = element_text(size = 10, color = col_muted),
        plot.caption  = element_text(size = 8,  color = col_muted, hjust = 0),
        strip.text = element_text(face = "bold", size = 10.5, color = col_ink, hjust = 0),
        legend.position = "none")

# ---- data --------------------------------------------------------------------
Sys.setlocale("LC_TIME", "C")  # month abbreviations in jobs.csv are English
jobs <- read.csv("jobs.csv")
d <- as.Date(jobs$posted, format = "%Y-%b-%d")
yr <- as.integer(format(d, "%Y")); mo <- as.integer(format(d, "%m"))
jobs$season <- ifelse(mo >= 7, yr, yr - 1)
tt <- subset(jobs, tenure_line == 1 & season %in% 2016:2025)

season_n <- table(factor(tt$season, levels = 2016:2025))

# one row per post × subfield
long <- data.frame(season = rep(tt$season, lengths(strsplit(tt$fields, "|", fixed = TRUE))),
                   field  = unlist(strsplit(tt$fields, "|", fixed = TRUE)))
counts <- as.data.frame(table(field = long$field, season = factor(long$season, levels = 2016:2025)),
                        stringsAsFactors = FALSE)
totals <- tapply(counts$Freq, counts$field, sum)
keep   <- names(totals)[totals >= 10]
counts <- subset(counts, field %in% keep)
counts$field <- factor(counts$field, levels = names(sort(totals[keep])))  # largest on top
counts$share <- 100 * counts$Freq / as.numeric(season_n[counts$season])

seasons <- sprintf("%d–%02d", 2016:2025, (2017:2026) %% 100)
counts$season_lab <- factor(seasons[as.integer(counts$season) - 2015], levels = seasons)

caption <- paste0(
  "A post that lists several subfields counts once in each. Subfields with fewer than 10 posts in total are not shown.\n",
  "Source: linguistlist.org/issues, topic = Jobs. Data and scripts: gdgarcia.ca")

# ---- figures -----------------------------------------------------------------
heatmap <- function(df, value, title, subtitle) {
  df$value <- df[[value]]
  ggplot(df, aes(season_lab, field, fill = value / max(value))) +
    geom_tile(color = "white", linewidth = 0.8) +
    geom_text(aes(label = round(value), color = value / max(value) > 0.55), size = 3.6) +
    scale_fill_gradientn(colors = ramp, limits = c(0, 1)) +
    scale_color_manual(values = c(`TRUE` = "white", `FALSE` = col_ink)) +
    scale_x_discrete(expand = c(0, 0)) +
    scale_y_discrete(expand = c(0, 0)) +
    labs(title = title, subtitle = subtitle, caption = caption) +
    base_theme +
    theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 11),
          axis.text.y = element_text(size = 11.5),
          plot.title = element_text(face = "bold", size = 15, color = col_ink),
          plot.subtitle = element_text(size = 11.5, color = col_muted),
          plot.caption = element_text(size = 9, color = col_muted, hjust = 0))
}

p1 <- heatmap(counts, "Freq",
              "Tenure-track job posts on LINGUIST List by subfield",
              paste0("Number of posts per job-market season (July–June).\nTotal tenure-track posts per season: ",
                     paste(as.integer(season_n), collapse = ", "), "."))
p2 <- heatmap(counts, "share",
              "Share of each season's tenure-track posts on LINGUIST List, by subfield",
              "Percentage of the season's tenure-track posts that list the subfield (July–June seasons).")

ggsave(file.path(out_dir, "tt_counts.png"), p1, width = 10, height = 9, dpi = 300, bg = "white")
ggsave(file.path(out_dir, "tt_shares.png"), p2, width = 10, height = 9, dpi = 300, bg = "white")
