Skip to content

Commit 1d8483a

Browse files
Improve error when piping plots into facets (#4385)
Resolves #4379
1 parent 199be05 commit 1d8483a

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
installed, which will offer to install the package before continuing (#4375,
2929
@malcolmbarrett)
3030

31+
* Improved error with hint when piping a `ggplot` object into a facet function
32+
(#4379, @mitchelloharawild).
33+
3134
# ggplot2 3.3.3
3235
This is a small patch release mainly intended to address changes in R and CRAN.
3336
It further changes the licensing model of ggplot2 to an MIT license.

R/facet-.r

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,7 @@ df.grid <- function(a, b) {
276276
# facetting variables.
277277

278278
as_facets_list <- function(x) {
279-
if (inherits(x, "uneval")) {
280-
abort("Please use `vars()` to supply facet variables")
281-
}
279+
x <- validate_facets(x)
282280
if (is_quosures(x)) {
283281
x <- quos_auto_name(x)
284282
return(list(x))
@@ -315,6 +313,19 @@ as_facets_list <- function(x) {
315313
x
316314
}
317315

316+
validate_facets <- function(x) {
317+
if (inherits(x, "uneval")) {
318+
abort("Please use `vars()` to supply facet variables")
319+
}
320+
if (inherits(x, "ggplot")) {
321+
abort(
322+
"Please use `vars()` to supply facet variables\nDid you use %>% instead of +?"
323+
)
324+
}
325+
x
326+
}
327+
328+
318329
# Flatten a list of quosures objects to a quosures object, and compact it
319330
compact_facets <- function(x) {
320331
x <- flatten_if(x, is_list)

R/facet-grid-.r

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ grid_as_facets_list <- function(rows, cols) {
157157
is_rows_vars <- is.null(rows) || is_quosures(rows)
158158
if (!is_rows_vars) {
159159
if (!is.null(cols)) {
160-
abort("`rows` must be `NULL` or a `vars()` list if `cols` is a `vars()` list")
160+
msg <- "`rows` must be `NULL` or a `vars()` list if `cols` is a `vars()` list"
161+
if(inherits(rows, "ggplot")) {
162+
msg <- paste0(
163+
msg, "\n",
164+
"Did you use %>% instead of +?"
165+
)
166+
}
167+
abort(msg)
161168
}
162169
# For backward-compatibility
163170
facets_list <- as_facets_list(rows)

R/facet-wrap.r

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
8686
x = any(scales %in% c("free_x", "free")),
8787
y = any(scales %in% c("free_y", "free"))
8888
)
89+
90+
# Check for deprecated labellers
91+
labeller <- check_labeller(labeller)
92+
93+
# Flatten all facets dimensions into a single one
94+
facets <- wrap_as_facets_list(facets)
95+
8996
if (!is.null(switch)) {
9097
.Deprecated("strip.position", old = "switch")
9198
strip.position <- if (switch == "x") "bottom" else "left"
@@ -102,12 +109,6 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
102109
ncol <- sanitise_dim(ncol)
103110
}
104111

105-
# Check for deprecated labellers
106-
labeller <- check_labeller(labeller)
107-
108-
# Flatten all facets dimensions into a single one
109-
facets <- wrap_as_facets_list(facets)
110-
111112
ggproto(NULL, FacetWrap,
112113
shrink = shrink,
113114
params = list(

0 commit comments

Comments
 (0)