R: add plot_model_comparison_no_gap.R mirroring 1-year script; generate per-plot and 2x2 cowplot grid from model_comparison_auc_no_gap.csv

This commit is contained in:
2025-10-22 15:43:16 +08:00
parent 9917b3ab63
commit 262a7db0da
2 changed files with 169 additions and 74 deletions

View File

@@ -7,6 +7,7 @@
suppressPackageStartupMessages({
library(ggplot2)
library(cowplot)
})
args <- commandArgs(trailingOnly = TRUE)
@@ -25,24 +26,6 @@ df <- tryCatch({
stop(sprintf("Failed to read CSV at '%s': %s", csv_path, e$message))
})
# Helper to make a scatter plot comparing model vs Delphi AUC
make_comparison_plot <- function(data, y_col, title_text, y_label) {
# Use shape 21 (filled circle) to allow white stroke and colored fill
ggplot(data, aes(x = auc_delphi, y = .data[[y_col]])) +
geom_abline(slope = 1, intercept = 0, color = "black", linetype = "dashed", linewidth = 0.5) +
geom_vline(xintercept = 0.5, color = "gray50", linetype = "dashed", linewidth = 0.4) +
geom_hline(yintercept = 0.5, color = "gray50", linetype = "dashed", linewidth = 0.4) +
geom_point(aes(fill = Colour), shape = 21, color = "white", stroke = 0.65, size = 2.2, alpha = 0.95, show.legend = FALSE) +
scale_fill_identity() +
coord_cartesian(xlim = c(0.3, 1.05), ylim = c(0.3, 1.05)) +
coord_fixed(ratio = 1) +
labs(title = title_text, x = "AUC_Delphi", y = y_label) +
theme_minimal(base_size = 10) +
theme(
plot.title = element_text(hjust = 0.5),
panel.grid.minor = element_blank()
)
}
# Helper to compare any two AUC columns (x vs y)
make_xy_plot <- function(data, x_col, y_col, title_text, x_label, y_label) {
@@ -62,66 +45,30 @@ make_xy_plot <- function(data, x_col, y_col, title_text, x_label, y_label) {
)
}
# Plot: AUC_256 vs AUC_Delphi (1 year gap)
if (!all(c("auc_delphi", "auc_256") %in% names(df))) {
stop("Input CSV must contain columns 'auc_delphi' and 'auc_256'.")
}
p256 <- make_comparison_plot(
data = df,
y_col = "auc_256",
title_text = "AUC_256 vs AUC_Delphi 1 year gap",
y_label = "AUC_256"
)
out_256 <- file.path(out_dir, "model_comparison_auc_256_vs_delphi_1year.png")
ggsave(filename = out_256, plot = p256, width = 7, height = 4, dpi = 600, bg = "white")
cat(sprintf("Saved: %s\n", out_256))
# Plot: AUC_120 vs AUC_Delphi (1 year gap)
if (!"auc_120" %in% names(df)) {
warning("Column 'auc_120' not found in CSV; skipping AUC_120 vs AUC_Delphi plot.")
} else {
p120 <- make_comparison_plot(
data = df,
y_col = "auc_120",
title_text = "AUC_120 vs AUC_Delphi 1 year gap",
y_label = "AUC_120"
# Helper to compare model AUC vs Delphi AUC (x = auc_delphi)
make_delphi_plot <- function(data, y_col, title_text, y_label) {
ggplot(data, aes(x = auc_delphi, y = .data[[y_col]])) +
geom_abline(slope = 1, intercept = 0, color = "black", linetype = "dashed", linewidth = 0.5) +
geom_vline(xintercept = 0.5, color = "gray50", linetype = "dashed", linewidth = 0.4) +
geom_hline(yintercept = 0.5, color = "gray50", linetype = "dashed", linewidth = 0.4) +
geom_point(aes(fill = Colour), shape = 21, color = "white", stroke = 0.65, size = 2.2, alpha = 0.95, show.legend = FALSE) +
scale_fill_identity() +
coord_cartesian(xlim = c(0.3, 1.05), ylim = c(0.3, 1.05)) +
coord_fixed(ratio = 1) +
labs(title = title_text, x = "AUC_Delphi", y = y_label) +
theme_minimal(base_size = 10) +
theme(
plot.title = element_text(hjust = 0.5),
panel.grid.minor = element_blank()
)
out_120 <- file.path(out_dir, "fig_auc_120_vs_delphi_1year.png")
ggsave(filename = out_120, plot = p120, width = 7, height = 4, dpi = 600, bg = "white")
cat(sprintf("Saved: %s\n", out_120))
}
# Plot: AUC_256_L vs AUC_Delphi (1 year gap)
if (!"auc_256_l" %in% names(df)) {
warning("Column 'auc_256_l' not found in CSV; skipping AUC_256_L vs AUC_Delphi plot.")
} else {
p256l <- make_comparison_plot(
data = df,
y_col = "auc_256_l",
title_text = "AUC_256_L vs AUC_Delphi 1 year gap",
y_label = "AUC_256_L"
)
out_256l <- file.path(out_dir, "model_comparison_auc_256_l_vs_delphi_1year.png")
ggsave(filename = out_256l, plot = p256l, width = 7, height = 4, dpi = 600, bg = "white")
cat(sprintf("Saved: %s\n", out_256l))
# Placeholder empty plot if a required column is missing
empty_plot <- function(msg) {
ggplot() + theme_void() + ggtitle(msg) + theme(plot.title = element_text(hjust = 0.5))
}
# Plot: AUC_120_L vs AUC_Delphi (1 year gap)
if (!"auc_120_l" %in% names(df)) {
warning("Column 'auc_120_l' not found in CSV; skipping AUC_120_L vs AUC_Delphi plot.")
} else {
p120l <- make_comparison_plot(
data = df,
y_col = "auc_120_l",
title_text = "AUC_120_L vs AUC_Delphi 1 year gap",
y_label = "AUC_120_L"
)
out_120l <- file.path(out_dir, "fig_auc_120_l_vs_delphi_1year.png")
ggsave(filename = out_120l, plot = p120l, width = 7, height = 4, dpi = 600, bg = "white")
cat(sprintf("Saved: %s\n", out_120l))
}
# Plot: AUC_120 vs AUC_120_L (1 year gap)
if (!all(c("auc_120", "auc_120_l") %in% names(df))) {
@@ -156,3 +103,23 @@ if (!all(c("auc_256", "auc_256_l") %in% names(df))) {
ggsave(filename = out_256_vs_256l, plot = p256_vs_256l, width = 7, height = 4, dpi = 600, bg = "white")
cat(sprintf("Saved: %s\n", out_256_vs_256l))
}
# ---- Combined 2x2 grid: (auc_120 vs delphi), (auc_256 vs delphi), (auc_120_l vs delphi), (auc_256_l vs delphi) ----
has_cols <- function(cols) all(cols %in% names(df))
p_120_vs_delphi <- if (has_cols(c("auc_delphi", "auc_120"))) make_delphi_plot(df, "auc_120", "AUC_120 vs Delphi (1 year)", "AUC_120") else empty_plot("Missing auc_120 or auc_delphi")
p_256_vs_delphi <- if (has_cols(c("auc_delphi", "auc_256"))) make_delphi_plot(df, "auc_256", "AUC_256 vs Delphi (1 year)", "AUC_256") else empty_plot("Missing auc_256 or auc_delphi")
p_120l_vs_delphi <- if (has_cols(c("auc_delphi", "auc_120_l"))) make_delphi_plot(df, "auc_120_l", "AUC_120_L vs Delphi (1 year)", "AUC_120_L") else empty_plot("Missing auc_120_l or auc_delphi")
p_256l_vs_delphi <- if (has_cols(c("auc_delphi", "auc_256_l"))) make_delphi_plot(df, "auc_256_l", "AUC_256_L vs Delphi (1 year)", "AUC_256_L") else empty_plot("Missing auc_256_l or auc_delphi")
grid_plot <- plot_grid(
p_120_vs_delphi, p_256_vs_delphi,
p_120l_vs_delphi, p_256l_vs_delphi,
labels = c("A", "B", "C", "D"),
ncol = 2, align = "hv"
)
out_grid <- file.path(out_dir, "model_comparison_auc_vs_delphi_1year_grid.png")
ggsave(filename = out_grid, plot = grid_plot, width = 12, height = 8, dpi = 300, bg = "white")
cat(sprintf("Saved grid: %s\n", out_grid))

View File

@@ -0,0 +1,128 @@
# Plot AUC comparisons (no gap) between models and Delphi using ggplot2
# Usage:
# Rscript plot_model_comparison_no_gap.R [path_to_csv] [output_dir]
# Defaults:
# path_to_csv = "model_comparison_auc_no_gap.csv"
# output_dir = current working directory (".")
suppressPackageStartupMessages({
library(ggplot2)
library(cowplot)
})
args <- commandArgs(trailingOnly = TRUE)
csv_path <- if (length(args) >= 1) args[1] else "model_comparison_auc_no_gap.csv"
out_dir <- if (length(args) >= 2) args[2] else "."
if (!dir.exists(out_dir)) {
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)
}
# Read data
# Expect columns including: auc_delphi, auc_256, auc_120, auc_256_l, auc_120_l, Colour (hex color), name, etc.
df <- tryCatch({
read.csv(csv_path, check.names = FALSE)
}, error = function(e) {
stop(sprintf("Failed to read CSV at '%s': %s", csv_path, e$message))
})
# Helper to compare any two AUC columns (x vs y)
make_xy_plot <- function(data, x_col, y_col, title_text, x_label, y_label) {
ggplot(data, aes(x = .data[[x_col]], y = .data[[y_col]])) +
geom_abline(slope = 1, intercept = 0, color = "black", linetype = "dashed", linewidth = 0.5) +
geom_vline(xintercept = 0.5, color = "gray50", linetype = "dashed", linewidth = 0.4) +
geom_hline(yintercept = 0.5, color = "gray50", linetype = "dashed", linewidth = 0.4) +
geom_point(aes(fill = Colour), shape = 21, color = "white", stroke = 0.65, size = 2.2, alpha = 0.95, show.legend = FALSE) +
scale_fill_identity() +
coord_cartesian(xlim = c(0.3, 1.05), ylim = c(0.3, 1.05)) +
coord_fixed(ratio = 1) +
labs(title = title_text, x = x_label, y = y_label) +
theme_minimal(base_size = 10) +
theme(
plot.title = element_text(hjust = 0.5),
panel.grid.minor = element_blank()
)
}
# Helper to compare model AUC vs Delphi AUC (x = auc_delphi)
make_delphi_plot <- function(data, y_col, title_text, y_label) {
ggplot(data, aes(x = auc_delphi, y = .data[[y_col]])) +
geom_abline(slope = 1, intercept = 0, color = "black", linetype = "dashed", linewidth = 0.5) +
geom_vline(xintercept = 0.5, color = "gray50", linetype = "dashed", linewidth = 0.4) +
geom_hline(yintercept = 0.5, color = "gray50", linetype = "dashed", linewidth = 0.4) +
geom_point(aes(fill = Colour), shape = 21, color = "white", stroke = 0.65, size = 2.2, alpha = 0.95, show.legend = FALSE) +
scale_fill_identity() +
coord_cartesian(xlim = c(0.3, 1.05), ylim = c(0.3, 1.05)) +
coord_fixed(ratio = 1) +
labs(title = title_text, x = "AUC_Delphi", y = y_label) +
theme_minimal(base_size = 10) +
theme(
plot.title = element_text(hjust = 0.5),
panel.grid.minor = element_blank()
)
}
# Placeholder empty plot if a required column is missing
empty_plot <- function(msg) {
ggplot() + theme_void() + ggtitle(msg) + theme(plot.title = element_text(hjust = 0.5))
}
# Individual Delphi comparison plots
has_cols <- function(cols) all(cols %in% names(df))
# AUC_120 vs AUC_Delphi (no gap)
if (has_cols(c("auc_delphi", "auc_120"))) {
p120 <- make_delphi_plot(df, "auc_120", "AUC_120 vs AUC_Delphi (no gap)", "AUC_120")
out_120 <- file.path(out_dir, "fig_auc_120_vs_delphi_no_gap.png")
ggsave(filename = out_120, plot = p120, width = 7, height = 4, dpi = 600, bg = "white")
cat(sprintf("Saved: %s\n", out_120))
} else {
warning("Missing columns for AUC_120 vs Delphi plot.")
}
# AUC_256 vs AUC_Delphi (no gap)
if (has_cols(c("auc_delphi", "auc_256"))) {
p256 <- make_delphi_plot(df, "auc_256", "AUC_256 vs AUC_Delphi (no gap)", "AUC_256")
out_256 <- file.path(out_dir, "model_comparison_auc_256_vs_delphi_no_gap.png")
ggsave(filename = out_256, plot = p256, width = 7, height = 4, dpi = 600, bg = "white")
cat(sprintf("Saved: %s\n", out_256))
} else {
warning("Missing columns for AUC_256 vs Delphi plot.")
}
# AUC_120_L vs AUC_Delphi (no gap)
if (has_cols(c("auc_delphi", "auc_120_l"))) {
p120l <- make_delphi_plot(df, "auc_120_l", "AUC_120_L vs AUC_Delphi (no gap)", "AUC_120_L")
out_120l <- file.path(out_dir, "fig_auc_120_l_vs_delphi_no_gap.png")
ggsave(filename = out_120l, plot = p120l, width = 7, height = 4, dpi = 600, bg = "white")
cat(sprintf("Saved: %s\n", out_120l))
} else {
warning("Missing columns for AUC_120_L vs Delphi plot.")
}
# AUC_256_L vs AUC_Delphi (no gap)
if (has_cols(c("auc_delphi", "auc_256_l"))) {
p256l <- make_delphi_plot(df, "auc_256_l", "AUC_256_L vs AUC_Delphi (no gap)", "AUC_256_L")
out_256l <- file.path(out_dir, "model_comparison_auc_256_l_vs_delphi_no_gap.png")
ggsave(filename = out_256l, plot = p256l, width = 7, height = 4, dpi = 600, bg = "white")
cat(sprintf("Saved: %s\n", out_256l))
} else {
warning("Missing columns for AUC_256_L vs Delphi plot.")
}
# 2x2 grid of Delphi comparisons
p_120_vs_delphi <- if (has_cols(c("auc_delphi", "auc_120"))) make_delphi_plot(df, "auc_120", "AUC_120 vs Delphi (no gap)", "AUC_120") else empty_plot("Missing auc_120 or auc_delphi")
p_256_vs_delphi <- if (has_cols(c("auc_delphi", "auc_256"))) make_delphi_plot(df, "auc_256", "AUC_256 vs Delphi (no gap)", "AUC_256") else empty_plot("Missing auc_256 or auc_delphi")
p_120l_vs_delphi <- if (has_cols(c("auc_delphi", "auc_120_l"))) make_delphi_plot(df, "auc_120_l", "AUC_120_L vs Delphi (no gap)", "AUC_120_L") else empty_plot("Missing auc_120_l or auc_delphi")
p_256l_vs_delphi <- if (has_cols(c("auc_delphi", "auc_256_l"))) make_delphi_plot(df, "auc_256_l", "AUC_256_L vs Delphi (no gap)", "AUC_256_L") else empty_plot("Missing auc_256_l or auc_delphi")
grid_plot <- plot_grid(
p_120_vs_delphi, p_256_vs_delphi,
p_120l_vs_delphi, p_256l_vs_delphi,
labels = c("A", "B", "C", "D"),
ncol = 2, align = "hv"
)
out_grid <- file.path(out_dir, "model_comparison_auc_vs_delphi_no_gap_grid.png")
ggsave(filename = out_grid, plot = grid_plot, width = 12, height = 8, dpi = 300, bg = "white")
cat(sprintf("Saved grid: %s\n", out_grid))