Skip to content

Commit

Permalink
Warn about unknown aesthetics & params.
Browse files Browse the repository at this point in the history
Fixes #1585
  • Loading branch information
hadley committed Sep 23, 2016
1 parent 0eac37b commit befc4d0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# ggplot2 2.1.0.9000

* When creating a layer, ggplot2 will warn if you use an unknown aesthetic
or an unknown parameter. Compared to the previous version, this is
stricter for aesthetics (previously there was no message), and less
strict for parameters (previously this threw an error) (#1585).

* The facet system, as well as the internal panel class, has been rewritten in
ggproto. Facets are now extendable in the same manner as geoms, stats etc. and
the manner in which this is done is described in the extension vignette. On
Expand Down
23 changes: 20 additions & 3 deletions R/layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,28 @@ layer <- function(geom = NULL, stat = NULL,
stat_params <- params[intersect(names(params), stat$parameters(TRUE))]

all <- c(geom$parameters(TRUE), stat$parameters(TRUE), geom$aesthetics())
extra <- setdiff(names(params), all)
if (length(extra) > 0) {
stop("Unknown parameters: ", paste(extra, collapse = ", "), call. = FALSE)

# Warn about extra params and aesthetics
extra_param <- setdiff(names(params), all)
if (length(extra_param) > 0) {
warning(
"Ignoring unknown parameters: ", paste(extra_param, collapse = ", "),
call. = FALSE,
immediate. = TRUE
)
}

extra_aes <- setdiff(names(mapping), c(geom$aesthetics(), stat$aesthetics()))
if (length(extra_aes) > 0) {
warning(
"Ignoring unknown aesthetics: ", paste(extra_aes, collapse = ", "),
call. = FALSE,
immediate. = TRUE
)
}



ggproto("LayerInstance", Layer,
geom = geom,
geom_params = geom_params,
Expand Down
5 changes: 5 additions & 0 deletions R/stat-.r
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,10 @@ Stat <- ggproto("Stat",
args <- union(args, self$extra_params)
}
args
},

aesthetics = function(self) {
c(union(self$required_aes, names(self$default_aes)), "group")
}

)
8 changes: 6 additions & 2 deletions tests/testthat/test-layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ test_that("aesthetics go in aes_params", {
expect_equal(l$aes_params, list(size = "red"))
})

test_that("unknown params create error", {
expect_error(geom_point(blah = "red"), "Unknown parameters")
test_that("unknown params create warning", {
expect_warning(geom_point(blah = "red"), "unknown parameters")
})

test_that("unknown aesthietcs create warning", {
expect_warning(geom_point(aes(blah = "red")), "unknown aesthetics")
})

# Calculated aesthetics ---------------------------------------------------
Expand Down
13 changes: 9 additions & 4 deletions tests/testthat/test-stat-bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ test_that("stat_bin throws error when y aesthetic present", {
expect_error(ggplot_build(ggplot(dat, aes(x, y)) + stat_bin()),
"must not be used with a y aesthetic.")

expect_error(p <- ggplot_build(ggplot(dat, aes(x)) + stat_bin(y = 5)),
"Unknown parameters: y")
expect_warning(
expect_error(
ggplot_build(ggplot(dat, aes(x)) + stat_bin(y = 5)),
"StatBin requires a continuous x"
),
"unknown parameters: y"
)
})

test_that("bins specifies the number of bins", {
Expand Down Expand Up @@ -116,8 +121,8 @@ test_that("stat_count throws error when y aesthetic present", {
expect_error(ggplot_build(ggplot(dat, aes(x, y)) + stat_count()),
"must not be used with a y aesthetic.")

expect_error(p <- ggplot_build(ggplot(dat, aes(x)) + stat_count(y = 5)),
"Unknown parameters: y")
expect_warning(p <- ggplot_build(ggplot(dat, aes(x)) + stat_count(y = 5)),
"unknown parameters: y")
})

test_that("stat_count preserves x order for continuous and discrete", {
Expand Down

0 comments on commit befc4d0

Please sign in to comment.