Skip to content

Commit 8c724a4

Browse files
authored
Make facet section of summary() more terse (#6074)
* print facets vars instead of whole object * add test * add news bullet
1 parent eec7490 commit 8c724a4

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* The `summary()` method for ggplots is now more terse about facets
4+
(@teunbrand, #5989).
35
* `guide_bins()`, `guide_colourbar()` and `guide_coloursteps()` gain an `angle`
46
argument to overrule theme settings, similar to `guide_axis(angle)`
57
(@teunbrand, #4594).

R/summary.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ summary.ggplot <- function(object, ...) {
2929
cat("scales: ", paste(object$scales$input(), collapse = ", "), "\n")
3030
}
3131

32-
cat("faceting: ")
33-
print(object$facet)
32+
vars <- object$facet$vars()
33+
vars <- if (length(vars) > 0) paste0("~", vars) else "<empty>"
34+
cat("faceting: ", paste0(vars, collapse = ", "), "\n")
3435

3536
if (length(object$layers) > 0)
3637
cat("-----------------------------------\n")

tests/testthat/_snaps/utilities.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,19 @@
5454

5555
Can't recycle `..1` (size 4) to match `..2` (size 0).
5656

57+
# summary method gives a nice summary
58+
59+
Code
60+
summary(p)
61+
Output
62+
data: manufacturer, model, displ, year, cyl, trans, drv, cty, hwy, fl,
63+
class [234x11]
64+
mapping: x = ~displ, y = ~hwy, colour = ~drv
65+
scales: x, xmin, xmax, xend, xintercept, xmin_final, xmax_final, xlower, xmiddle, xupper, x0, colour
66+
faceting: ~year, ~cyl
67+
-----------------------------------
68+
geom_point: na.rm = FALSE
69+
stat_identity: na.rm = FALSE
70+
position_identity
71+
72+

tests/testthat/test-utilities.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,16 @@ test_that("expose/ignore_data() can round-trip a data.frame", {
196196
expect_equal(test, df[, c("a", "c", "b", "d")])
197197

198198
})
199+
200+
test_that("summary method gives a nice summary", {
201+
# This test isn't important enough to break anything on CRAN
202+
skip_on_cran()
203+
204+
p <- ggplot(mpg, aes(displ, hwy, colour = drv)) +
205+
geom_point() +
206+
scale_x_continuous() +
207+
scale_colour_brewer() +
208+
facet_grid(year ~ cyl)
209+
210+
expect_snapshot(summary(p))
211+
})

0 commit comments

Comments
 (0)