R: add orientation arg to boxplot script; toggle coord_flip for rotated (horizontal) vs vertical layouts and adjust axis labels

This commit is contained in:
2025-10-22 15:58:15 +08:00
parent f8e0104d6b
commit b954b4b3e7

View File

@@ -15,6 +15,7 @@ args <- commandArgs(trailingOnly = TRUE)
one_year_csv <- if (length(args) >= 1) args[1] else "model_comparison_auc_1year.csv" one_year_csv <- if (length(args) >= 1) args[1] else "model_comparison_auc_1year.csv"
no_gap_csv <- if (length(args) >= 2) args[2] else "model_comparison_auc_no_gap.csv" no_gap_csv <- if (length(args) >= 2) args[2] else "model_comparison_auc_no_gap.csv"
out_dir <- if (length(args) >= 3) args[3] else "." out_dir <- if (length(args) >= 3) args[3] else "."
orientation <- if (length(args) >= 4) tolower(args[4]) else "horizontal" # "horizontal" (flipped) or "vertical"
if (!dir.exists(out_dir)) { if (!dir.exists(out_dir)) {
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE) dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)
@@ -83,7 +84,7 @@ build_long_df <- function(df) {
} }
# Make the boxplot grouped by chapter # Make the boxplot grouped by chapter
make_boxplot <- function(long_df, title_text) { make_boxplot <- function(long_df, title_text, flip = TRUE) {
# Order chapters by median AUC of Delphi if available, otherwise overall median # Order chapters by median AUC of Delphi if available, otherwise overall median
has_delphi <- any(long_df$model == "Delphi") has_delphi <- any(long_df$model == "Delphi")
if (has_delphi) { if (has_delphi) {
@@ -94,9 +95,8 @@ make_boxplot <- function(long_df, title_text) {
chap_levels <- med[order(med$auc, decreasing = TRUE), "chapter"] chap_levels <- med[order(med$auc, decreasing = TRUE), "chapter"]
long_df$chapter <- factor(long_df$chapter, levels = chap_levels) long_df$chapter <- factor(long_df$chapter, levels = chap_levels)
ggplot(long_df, aes(x = chapter, y = auc, fill = model)) + p <- ggplot(long_df, aes(x = chapter, y = auc, fill = model)) +
geom_boxplot(outlier.shape = 19, outlier.size = 0.7, width = 0.75, alpha = 0.95) + geom_boxplot(outlier.shape = 19, outlier.size = 0.7, width = 0.75, alpha = 0.95) +
coord_flip() +
scale_y_continuous(limits = c(0.3, 1.0), breaks = seq(0.3, 1.0, by = 0.1)) + scale_y_continuous(limits = c(0.3, 1.0), breaks = seq(0.3, 1.0, by = 0.1)) +
labs(title = title_text, x = "ICD-10 Chapter", y = "AUC") + labs(title = title_text, x = "ICD-10 Chapter", y = "AUC") +
theme_minimal(base_size = 11) + theme_minimal(base_size = 11) +
@@ -106,6 +106,13 @@ make_boxplot <- function(long_df, title_text) {
legend.position = "bottom" legend.position = "bottom"
) + ) +
guides(fill = guide_legend(nrow = 1)) guides(fill = guide_legend(nrow = 1))
if (flip) {
p <- p + coord_flip()
} else {
# For vertical plots, angle x-axis labels for readability
p <- p + theme(axis.text.x = element_text(angle = 45, hjust = 1))
}
p
} }
# Build plots for 1-year and no-gap # Build plots for 1-year and no-gap
@@ -115,8 +122,10 @@ no_gap_df <- read_csv_safe(no_gap_csv)
one_year_long <- build_long_df(one_year_df) one_year_long <- build_long_df(one_year_df)
no_gap_long <- build_long_df(no_gap_df) no_gap_long <- build_long_df(no_gap_df)
p1 <- make_boxplot(one_year_long, "AUC by ICD-10 Chapter (1-year gap)") flip_flag <- ifelse(orientation %in% c("horizontal", "flip", "flipped"), TRUE, FALSE)
p2 <- make_boxplot(no_gap_long, "AUC by ICD-10 Chapter (no gap)")
p1 <- make_boxplot(one_year_long, "AUC by ICD-10 Chapter (1-year gap)", flip = flip_flag)
p2 <- make_boxplot(no_gap_long, "AUC by ICD-10 Chapter (no gap)", flip = flip_flag)
# Save individual plots # Save individual plots
out_1year <- file.path(out_dir, "auc_boxplot_by_chapter_1year.png") out_1year <- file.path(out_dir, "auc_boxplot_by_chapter_1year.png")