From f48e19231c5e0f741276e378b3fb264f3aa73826 Mon Sep 17 00:00:00 2001 From: hurry060215-tech Date: Mon, 8 Jun 2026 20:18:56 +0800 Subject: [PATCH 1/9] Fix theme_blank plot margin --- R/themes.R | 24 ++++++++++++------------ tests/testthat.R | 4 ++++ tests/testthat/test-themes.R | 6 ++++++ 3 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-themes.R diff --git a/R/themes.R b/R/themes.R index 103897b..06b21e5 100644 --- a/R/themes.R +++ b/R/themes.R @@ -158,10 +158,10 @@ theme_blank <- function( legend.margin = margin(0, 0, 0, 0), legend.key.size = grid::unit(10, "pt"), plot.margin = margin( - lab_size + 2, - lab_size + 2, - lab_size + 2, - lab_size + 2, + lab_size + 8, + lab_size + 8, + lab_size + 8, + lab_size + 8, unit = "points" ) ) @@ -199,10 +199,10 @@ theme_blank <- function( ), grid::textGrob( label = xlab, - x = grid::unit(0, "npc"), - y = grid::unit(0, "npc"), - vjust = 4 / 3, - hjust = 0, + x = grid::unit(xlen_npc / 2, "npc"), + y = grid::unit(0, "npc") - grid::unit(0.8, "lines"), + vjust = 1, + hjust = 0.5, gp = grid::gpar(fontsize = lab_size) ), grid::linesGrob( @@ -212,10 +212,10 @@ theme_blank <- function( gp = grid::gpar(lwd = 2) ), grid::textGrob( - label = ylab, x = grid::unit(0, "npc"), - y = grid::unit(0, "npc"), - vjust = -2 / 3, - hjust = 0, + label = ylab, x = grid::unit(0, "npc") - grid::unit(0.8, "lines"), + y = grid::unit(ylen_npc / 2, "npc"), + vjust = 0, + hjust = 0.5, rot = 90, gp = grid::gpar(fontsize = lab_size) ) diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..818ccf8 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(thisplot) + +test_check("thisplot") diff --git a/tests/testthat/test-themes.R b/tests/testthat/test-themes.R new file mode 100644 index 0000000..907fcdf --- /dev/null +++ b/tests/testthat/test-themes.R @@ -0,0 +1,6 @@ +test_that("theme_blank leaves room for coordinate annotation", { + theme_layers <- thisplot::theme_blank(add_coord = FALSE, lab_size = 12) + plot_margin <- theme_layers[[1]][[1]]$plot.margin + + expect_equal(as.numeric(plot_margin), rep(20, 4)) +}) From 8071b4f2f0dd27f0a37915b1c9f1c09e0103f1d7 Mon Sep 17 00:00:00 2001 From: hurry060215-tech Date: Mon, 8 Jun 2026 21:19:48 +0800 Subject: [PATCH 2/9] Adjust theme_blank coordinate label positions for long text --- R/themes.R | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/R/themes.R b/R/themes.R index 06b21e5..327617d 100644 --- a/R/themes.R +++ b/R/themes.R @@ -199,10 +199,10 @@ theme_blank <- function( ), grid::textGrob( label = xlab, - x = grid::unit(xlen_npc / 2, "npc"), - y = grid::unit(0, "npc") - grid::unit(0.8, "lines"), + x = grid::unit(xlen_npc, "npc") + grid::unit(0.5, "lines"), + y = grid::unit(-1.2, "lines"), vjust = 1, - hjust = 0.5, + hjust = 0, gp = grid::gpar(fontsize = lab_size) ), grid::linesGrob( @@ -212,9 +212,10 @@ theme_blank <- function( gp = grid::gpar(lwd = 2) ), grid::textGrob( - label = ylab, x = grid::unit(0, "npc") - grid::unit(0.8, "lines"), - y = grid::unit(ylen_npc / 2, "npc"), - vjust = 0, + label = ylab, + x = grid::unit(-2, "lines"), + y = grid::unit(ylen_npc, "npc") + grid::unit(0.5, "lines"), + vjust = 0.5, hjust = 0.5, rot = 90, gp = grid::gpar(fontsize = lab_size) From d29a6cf7e4b2434da239a76ac1decd1efc79b7cf Mon Sep 17 00:00:00 2001 From: hurry060215-tech Date: Mon, 8 Jun 2026 21:26:15 +0800 Subject: [PATCH 3/9] Fine tune theme_blank coordinate label offsets for balanced spacing --- R/themes.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/themes.R b/R/themes.R index 327617d..24cc174 100644 --- a/R/themes.R +++ b/R/themes.R @@ -199,8 +199,8 @@ theme_blank <- function( ), grid::textGrob( label = xlab, - x = grid::unit(xlen_npc, "npc") + grid::unit(0.5, "lines"), - y = grid::unit(-1.2, "lines"), + x = grid::unit(xlen_npc, "npc") + grid::unit(0.15, "lines"), + y = grid::unit(-0.95, "lines"), vjust = 1, hjust = 0, gp = grid::gpar(fontsize = lab_size) @@ -213,8 +213,8 @@ theme_blank <- function( ), grid::textGrob( label = ylab, - x = grid::unit(-2, "lines"), - y = grid::unit(ylen_npc, "npc") + grid::unit(0.5, "lines"), + x = grid::unit(-1.0, "lines"), + y = grid::unit(ylen_npc, "npc") + grid::unit(0.15, "lines"), vjust = 0.5, hjust = 0.5, rot = 90, From adeec64a006416afd011235c193881ca6072991d Mon Sep 17 00:00:00 2001 From: hurry060215-tech Date: Mon, 8 Jun 2026 21:30:23 +0800 Subject: [PATCH 4/9] Nudge theme_blank x-axis label downward to avoid x-label overlap --- R/themes.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/themes.R b/R/themes.R index 24cc174..bc7189b 100644 --- a/R/themes.R +++ b/R/themes.R @@ -200,8 +200,8 @@ theme_blank <- function( grid::textGrob( label = xlab, x = grid::unit(xlen_npc, "npc") + grid::unit(0.15, "lines"), - y = grid::unit(-0.95, "lines"), - vjust = 1, + y = grid::unit(-1.05, "lines"), + vjust = 0, hjust = 0, gp = grid::gpar(fontsize = lab_size) ), From e1c44d07f199b52e800046e3119c0ea88b6f6c7c Mon Sep 17 00:00:00 2001 From: hurry060215-tech Date: Mon, 8 Jun 2026 21:32:37 +0800 Subject: [PATCH 5/9] Further tune x-axis label offset in theme_blank --- R/themes.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/themes.R b/R/themes.R index bc7189b..4869489 100644 --- a/R/themes.R +++ b/R/themes.R @@ -199,8 +199,8 @@ theme_blank <- function( ), grid::textGrob( label = xlab, - x = grid::unit(xlen_npc, "npc") + grid::unit(0.15, "lines"), - y = grid::unit(-1.05, "lines"), + x = grid::unit(xlen_npc, "npc") + grid::unit(0.35, "lines"), + y = grid::unit(-1.2, "lines"), vjust = 0, hjust = 0, gp = grid::gpar(fontsize = lab_size) From 34c1b0b49ab1d749bbd3f809dcaed57cab8a4b43 Mon Sep 17 00:00:00 2001 From: hurry060215-tech Date: Mon, 8 Jun 2026 21:34:08 +0800 Subject: [PATCH 6/9] Pull x-axis coordinate label slightly further from axis line --- R/themes.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/themes.R b/R/themes.R index 4869489..f5e43b5 100644 --- a/R/themes.R +++ b/R/themes.R @@ -200,7 +200,7 @@ theme_blank <- function( grid::textGrob( label = xlab, x = grid::unit(xlen_npc, "npc") + grid::unit(0.35, "lines"), - y = grid::unit(-1.2, "lines"), + y = grid::unit(-1.35, "lines"), vjust = 0, hjust = 0, gp = grid::gpar(fontsize = lab_size) From 6f75085dcfdf1dfcb8c7b3dd477a5a48831ece62 Mon Sep 17 00:00:00 2001 From: hurry060215-tech Date: Mon, 8 Jun 2026 21:39:29 +0800 Subject: [PATCH 7/9] Match theme_blank axis label offsets to upstream original layout --- R/themes.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/R/themes.R b/R/themes.R index f5e43b5..8d8b321 100644 --- a/R/themes.R +++ b/R/themes.R @@ -199,9 +199,9 @@ theme_blank <- function( ), grid::textGrob( label = xlab, - x = grid::unit(xlen_npc, "npc") + grid::unit(0.35, "lines"), - y = grid::unit(-1.35, "lines"), - vjust = 0, + x = grid::unit(0, "npc"), + y = grid::unit(0, "npc"), + vjust = 4 / 3, hjust = 0, gp = grid::gpar(fontsize = lab_size) ), @@ -213,10 +213,10 @@ theme_blank <- function( ), grid::textGrob( label = ylab, - x = grid::unit(-1.0, "lines"), - y = grid::unit(ylen_npc, "npc") + grid::unit(0.15, "lines"), - vjust = 0.5, - hjust = 0.5, + x = grid::unit(0, "npc"), + y = grid::unit(0, "npc"), + vjust = -2 / 3, + hjust = 0, rot = 90, gp = grid::gpar(fontsize = lab_size) ) From 5d09ce37775dbda0372f61e9a4915e5a68190ecf Mon Sep 17 00:00:00 2001 From: hurry Date: Mon, 8 Jun 2026 22:01:10 +0800 Subject: [PATCH 8/9] Revert "Fix:theme_blank plot margin" --- R/themes.R | 11 +++++------ tests/testthat.R | 4 ---- tests/testthat/test-themes.R | 6 ------ 3 files changed, 5 insertions(+), 16 deletions(-) delete mode 100644 tests/testthat.R delete mode 100644 tests/testthat/test-themes.R diff --git a/R/themes.R b/R/themes.R index 8d8b321..103897b 100644 --- a/R/themes.R +++ b/R/themes.R @@ -158,10 +158,10 @@ theme_blank <- function( legend.margin = margin(0, 0, 0, 0), legend.key.size = grid::unit(10, "pt"), plot.margin = margin( - lab_size + 8, - lab_size + 8, - lab_size + 8, - lab_size + 8, + lab_size + 2, + lab_size + 2, + lab_size + 2, + lab_size + 2, unit = "points" ) ) @@ -212,8 +212,7 @@ theme_blank <- function( gp = grid::gpar(lwd = 2) ), grid::textGrob( - label = ylab, - x = grid::unit(0, "npc"), + label = ylab, x = grid::unit(0, "npc"), y = grid::unit(0, "npc"), vjust = -2 / 3, hjust = 0, diff --git a/tests/testthat.R b/tests/testthat.R deleted file mode 100644 index 818ccf8..0000000 --- a/tests/testthat.R +++ /dev/null @@ -1,4 +0,0 @@ -library(testthat) -library(thisplot) - -test_check("thisplot") diff --git a/tests/testthat/test-themes.R b/tests/testthat/test-themes.R deleted file mode 100644 index 907fcdf..0000000 --- a/tests/testthat/test-themes.R +++ /dev/null @@ -1,6 +0,0 @@ -test_that("theme_blank leaves room for coordinate annotation", { - theme_layers <- thisplot::theme_blank(add_coord = FALSE, lab_size = 12) - plot_margin <- theme_layers[[1]][[1]]$plot.margin - - expect_equal(as.numeric(plot_margin), rep(20, 4)) -}) From 6ac20e7a7d746e4335bcb14f126aed2a9e36fa1c Mon Sep 17 00:00:00 2001 From: hurry060215-tech Date: Thu, 11 Jun 2026 18:05:28 +0800 Subject: [PATCH 9/9] Add x-axis text angle option to StatPlot --- R/StatPlot.R | 5 ++++- man/StatPlot.Rd | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/R/StatPlot.R b/R/StatPlot.R index 3718562..be112f3 100644 --- a/R/StatPlot.R +++ b/R/StatPlot.R @@ -74,6 +74,8 @@ #' Default is `"theme_this"`. #' @param theme_args Additional arguments to pass to the theme function. #' Default is `list()`. +#' @param x_text_angle Rotation angle for x-axis labels. +#' Default is `45`. #' @param grid_major Whether to show major panel grid lines. #' Default is `TRUE`. #' @param grid_major_colour Color of major panel grid lines. @@ -227,6 +229,7 @@ StatPlot <- function( legend.direction = "vertical", theme_use = "theme_this", theme_args = list(), + x_text_angle = 45, grid_major = TRUE, grid_major_colour = "grey80", grid_major_linetype = 2, @@ -965,7 +968,7 @@ StatPlot <- function( axis_text_x <- element_text() } else { axis_text_x <- element_text( - angle = 45, + angle = x_text_angle, hjust = 1, vjust = 1 ) diff --git a/man/StatPlot.Rd b/man/StatPlot.Rd index 269562e..97d879b 100644 --- a/man/StatPlot.Rd +++ b/man/StatPlot.Rd @@ -42,6 +42,7 @@ StatPlot( legend.direction = "vertical", theme_use = "theme_this", theme_args = list(), + x_text_angle = 45, grid_major = TRUE, grid_major_colour = "grey80", grid_major_linetype = 2, @@ -159,6 +160,9 @@ Default is \code{"theme_this"}.} \item{theme_args}{Additional arguments to pass to the theme function. Default is \code{list()}.} +\item{x_text_angle}{Rotation angle for x-axis labels. +Default is \code{45}.} + \item{grid_major}{Whether to show major panel grid lines. Default is \code{TRUE}.}