From 7ba5d628aa0f54bd322641fac459e394a2adc3d6 Mon Sep 17 00:00:00 2001 From: Fernando Alarid-Escudero Date: Fri, 13 Nov 2026 19:20:23 -0800 Subject: [PATCH] plot.exp_loss(): label every order of magnitude on the log-scale y axis With log_y = TRUE (the default), the y breaks came from number_ticks(), which spaces them evenly. On a log axis they bunched up at the top and left the lower curves without labels. Use scales::breaks_log(n_y_ticks) unless ybreaks is given. --- NEWS.md | 1 + R/exp_loss.R | 5 +++++ tests/testthat/test_elc.R | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/NEWS.md b/NEWS.md index dc7d086..69ce9ba 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,6 +22,7 @@ ## Bug fixes +- `plot.exp_loss()` with its default log-scale y axis (`log_y = TRUE`) now labels every order of magnitude. Its evenly spaced default breaks used to bunch up at the top of the log axis and leave the lower curves without any labels. - The `hund_strat` help page now describes the data (simulated costs and QALYs for 100 hypothetical strategies) and its columns, with an example. Typos in several help pages are fixed (#219). - `plot.psa(center = FALSE)` no longer warns "No shared levels found between `names(values)` of the manual scale and the data's fill values" when it is drawn. The fill scale is now added only when the center points that use it are. - `plot.owsa(col = "bw")` now draws each strategy with its own line type; all strategies used to share one line type and could not be told apart (#176). diff --git a/R/exp_loss.R b/R/exp_loss.R index 1bfc911..ceb63e8 100644 --- a/R/exp_loss.R +++ b/R/exp_loss.R @@ -145,6 +145,11 @@ plot.exp_loss <- function(x, # formatting if logging the y axis if (log_y) { tr <- "log10" + # evenly spaced breaks (number_ticks()) bunch up at the top of a log axis and leave its + # lower part without labels, so use breaks spread over the orders of magnitude instead + if (is.null(ybreaks)) { + ybreaks <- scales::breaks_log(n = n_y_ticks) + } } else { tr <- "identity" } diff --git a/tests/testthat/test_elc.R b/tests/testthat/test_elc.R index 0b8289e..013d06a 100644 --- a/tests/testthat/test_elc.R +++ b/tests/testthat/test_elc.R @@ -68,3 +68,22 @@ test_that("plot.exp_loss(col = 'bw') uses lsize and builds without warnings", { expect_equal(unique(b$data[[i_line]]$linewidth), 3) expect_equal(length(unique(b$data[[i_line]]$linetype)), length(unique(el$Strategy))) }) + +# with log_y = TRUE, the default y breaks were evenly spaced: they bunched up at the top of +# the log axis and left the lower orders of magnitude without labels +test_that("plot.exp_loss(log_y = TRUE) labels every order of magnitude of the y axis", { + el <- calc_exp_loss(psa_obj, wtp = seq(0, 2e5, by = 1e4)) + b <- ggplot2::ggplot_build(plot(el)) + v_breaks <- b$layout$panel_params[[1]]$y$breaks # on the log10 scale + v_breaks <- v_breaks[!is.na(v_breaks)] + v_range <- log10(range(el$Expected_Loss)) + # a labelled break in every order of magnitude the data cover + v_decades <- seq(ceiling(v_range[1]), floor(v_range[2])) + expect_true(all(v_decades %in% round(v_breaks, 10))) + # user-supplied breaks still win, and a linear axis keeps its evenly spaced breaks + b2 <- ggplot2::ggplot_build(plot(el, ybreaks = c(1e3, 1e4))) + expect_equal(10^b2$layout$panel_params[[1]]$y$breaks[!is.na(b2$layout$panel_params[[1]]$y$breaks)], + c(1e3, 1e4)) + b3 <- ggplot2::ggplot_build(plot(el, log_y = FALSE)) + expect_equal(diff(range(diff(stats::na.omit(b3$layout$panel_params[[1]]$y$breaks)))), 0) +})