Skip to content

Commit befc4d0

Browse files
committed
Warn about unknown aesthetics & params.
Fixes #1585
1 parent 0eac37b commit befc4d0

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# ggplot2 2.1.0.9000
2+
3+
* When creating a layer, ggplot2 will warn if you use an unknown aesthetic
4+
or an unknown parameter. Compared to the previous version, this is
5+
stricter for aesthetics (previously there was no message), and less
6+
strict for parameters (previously this threw an error) (#1585).
7+
28
* The facet system, as well as the internal panel class, has been rewritten in
39
ggproto. Facets are now extendable in the same manner as geoms, stats etc. and
410
the manner in which this is done is described in the extension vignette. On

R/layer.r

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,28 @@ layer <- function(geom = NULL, stat = NULL,
100100
stat_params <- params[intersect(names(params), stat$parameters(TRUE))]
101101

102102
all <- c(geom$parameters(TRUE), stat$parameters(TRUE), geom$aesthetics())
103-
extra <- setdiff(names(params), all)
104-
if (length(extra) > 0) {
105-
stop("Unknown parameters: ", paste(extra, collapse = ", "), call. = FALSE)
103+
104+
# Warn about extra params and aesthetics
105+
extra_param <- setdiff(names(params), all)
106+
if (length(extra_param) > 0) {
107+
warning(
108+
"Ignoring unknown parameters: ", paste(extra_param, collapse = ", "),
109+
call. = FALSE,
110+
immediate. = TRUE
111+
)
106112
}
107113

114+
extra_aes <- setdiff(names(mapping), c(geom$aesthetics(), stat$aesthetics()))
115+
if (length(extra_aes) > 0) {
116+
warning(
117+
"Ignoring unknown aesthetics: ", paste(extra_aes, collapse = ", "),
118+
call. = FALSE,
119+
immediate. = TRUE
120+
)
121+
}
122+
123+
124+
108125
ggproto("LayerInstance", Layer,
109126
geom = geom,
110127
geom_params = geom_params,

R/stat-.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,10 @@ Stat <- ggproto("Stat",
134134
args <- union(args, self$extra_params)
135135
}
136136
args
137+
},
138+
139+
aesthetics = function(self) {
140+
c(union(self$required_aes, names(self$default_aes)), "group")
137141
}
142+
138143
)

tests/testthat/test-layer.r

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ test_that("aesthetics go in aes_params", {
88
expect_equal(l$aes_params, list(size = "red"))
99
})
1010

11-
test_that("unknown params create error", {
12-
expect_error(geom_point(blah = "red"), "Unknown parameters")
11+
test_that("unknown params create warning", {
12+
expect_warning(geom_point(blah = "red"), "unknown parameters")
13+
})
14+
15+
test_that("unknown aesthietcs create warning", {
16+
expect_warning(geom_point(aes(blah = "red")), "unknown aesthetics")
1317
})
1418

1519
# Calculated aesthetics ---------------------------------------------------

tests/testthat/test-stat-bin.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ test_that("stat_bin throws error when y aesthetic present", {
66
expect_error(ggplot_build(ggplot(dat, aes(x, y)) + stat_bin()),
77
"must not be used with a y aesthetic.")
88

9-
expect_error(p <- ggplot_build(ggplot(dat, aes(x)) + stat_bin(y = 5)),
10-
"Unknown parameters: y")
9+
expect_warning(
10+
expect_error(
11+
ggplot_build(ggplot(dat, aes(x)) + stat_bin(y = 5)),
12+
"StatBin requires a continuous x"
13+
),
14+
"unknown parameters: y"
15+
)
1116
})
1217

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

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

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

0 commit comments

Comments
 (0)