Skip to content

Commit

Permalink
Make facet section of summary() more terse (#6074)
Browse files Browse the repository at this point in the history
* print facets vars instead of whole object

* add test

* add news bullet
  • Loading branch information
teunbrand committed Sep 9, 2024
1 parent eec7490 commit 8c724a4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
5 changes: 3 additions & 2 deletions R/summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<empty>"
cat("faceting: ", paste0(vars, collapse = ", "), "\n")

if (length(object$layers) > 0)
cat("-----------------------------------\n")
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

13 changes: 13 additions & 0 deletions tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

0 comments on commit 8c724a4

Please sign in to comment.