From cfd12e5d7dcd1e65b8e059c4ff8ad9563f16a8e4 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 4 Sep 2024 10:17:00 +0200 Subject: [PATCH 1/3] print facets vars instead of whole object --- R/summary.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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") From 45a769d453178dca0f4b3ce083e97a0a3cca4dd6 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 4 Sep 2024 10:17:09 +0200 Subject: [PATCH 2/3] add test --- tests/testthat/_snaps/utilities.md | 16 ++++++++++++++++ tests/testthat/test-utilities.R | 13 +++++++++++++ 2 files changed, 29 insertions(+) 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)) +}) From 3529435c074989e0dfee2daec160063aa6417072 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 4 Sep 2024 10:17:17 +0200 Subject: [PATCH 3/3] add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) 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).