From 8c724a4576580fa21ede15d82ca713be0ad508b9 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 9 Sep 2024 13:12:33 +0200 Subject: [PATCH] Make facet section of `summary()` more terse (#6074) * print facets vars instead of whole object * add test * add news bullet --- NEWS.md | 2 ++ R/summary.R | 5 +++-- tests/testthat/_snaps/utilities.md | 16 ++++++++++++++++ tests/testthat/test-utilities.R | 13 +++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 40bdb3d156..a4bce3f6be 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* The `summary()` method for ggplots is now more terse about facets + (@teunbrand, #5989). * `guide_bins()`, `guide_colourbar()` and `guide_coloursteps()` gain an `angle` argument to overrule theme settings, similar to `guide_axis(angle)` (@teunbrand, #4594). diff --git a/R/summary.R b/R/summary.R index e5422f8ad4..4a227a3599 100644 --- a/R/summary.R +++ b/R/summary.R @@ -29,8 +29,9 @@ summary.ggplot <- function(object, ...) { cat("scales: ", paste(object$scales$input(), collapse = ", "), "\n") } - cat("faceting: ") - print(object$facet) + vars <- object$facet$vars() + vars <- if (length(vars) > 0) paste0("~", vars) else "" + cat("faceting: ", paste0(vars, collapse = ", "), "\n") if (length(object$layers) > 0) cat("-----------------------------------\n") diff --git a/tests/testthat/_snaps/utilities.md b/tests/testthat/_snaps/utilities.md index 0101c1edd1..4560379f1e 100644 --- a/tests/testthat/_snaps/utilities.md +++ b/tests/testthat/_snaps/utilities.md @@ -54,3 +54,19 @@ Can't recycle `..1` (size 4) to match `..2` (size 0). +# summary method gives a nice summary + + Code + summary(p) + Output + data: manufacturer, model, displ, year, cyl, trans, drv, cty, hwy, fl, + class [234x11] + mapping: x = ~displ, y = ~hwy, colour = ~drv + scales: x, xmin, xmax, xend, xintercept, xmin_final, xmax_final, xlower, xmiddle, xupper, x0, colour + faceting: ~year, ~cyl + ----------------------------------- + geom_point: na.rm = FALSE + stat_identity: na.rm = FALSE + position_identity + + diff --git a/tests/testthat/test-utilities.R b/tests/testthat/test-utilities.R index 08e948ca82..315ca88ebc 100644 --- a/tests/testthat/test-utilities.R +++ b/tests/testthat/test-utilities.R @@ -196,3 +196,16 @@ test_that("expose/ignore_data() can round-trip a data.frame", { expect_equal(test, df[, c("a", "c", "b", "d")]) }) + +test_that("summary method gives a nice summary", { + # This test isn't important enough to break anything on CRAN + skip_on_cran() + + p <- ggplot(mpg, aes(displ, hwy, colour = drv)) + + geom_point() + + scale_x_continuous() + + scale_colour_brewer() + + facet_grid(year ~ cyl) + + expect_snapshot(summary(p)) +})