From b954b4b3e7a7913d06f1cfcb89e5765bf0545b82 Mon Sep 17 00:00:00 2001 From: Jiarui Li Date: Wed, 22 Oct 2025 15:58:15 +0800 Subject: [PATCH] R: add orientation arg to boxplot script; toggle coord_flip for rotated (horizontal) vs vertical layouts and adjust axis labels --- plot_auc_boxplots_by_chapter.R | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/plot_auc_boxplots_by_chapter.R b/plot_auc_boxplots_by_chapter.R index 8013282..47e85ee 100644 --- a/plot_auc_boxplots_by_chapter.R +++ b/plot_auc_boxplots_by_chapter.R @@ -15,6 +15,7 @@ args <- commandArgs(trailingOnly = TRUE) 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" 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)) { dir.create(out_dir, recursive = TRUE, showWarnings = FALSE) @@ -83,7 +84,7 @@ build_long_df <- function(df) { } # 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 has_delphi <- any(long_df$model == "Delphi") if (has_delphi) { @@ -94,9 +95,8 @@ make_boxplot <- function(long_df, title_text) { chap_levels <- med[order(med$auc, decreasing = TRUE), "chapter"] 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) + - coord_flip() + 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") + theme_minimal(base_size = 11) + @@ -106,6 +106,13 @@ make_boxplot <- function(long_df, title_text) { legend.position = "bottom" ) + 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 @@ -115,8 +122,10 @@ no_gap_df <- read_csv_safe(no_gap_csv) one_year_long <- build_long_df(one_year_df) no_gap_long <- build_long_df(no_gap_df) -p1 <- make_boxplot(one_year_long, "AUC by ICD-10 Chapter (1-year gap)") -p2 <- make_boxplot(no_gap_long, "AUC by ICD-10 Chapter (no gap)") +flip_flag <- ifelse(orientation %in% c("horizontal", "flip", "flipped"), TRUE, FALSE) + +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 out_1year <- file.path(out_dir, "auc_boxplot_by_chapter_1year.png")