-
Notifications
You must be signed in to change notification settings - Fork 2.1k
allow empty facet specs #3162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow empty facet specs #3162
Changes from 18 commits
f2aeb9d
36829a8
81dcbc1
0e2d33f
8719f91
075c328
e14a371
45db931
fb3ce9a
0d0dd33
ba757c6
16e294d
eda75d9
d17819f
2d735e2
675d1ac
6537482
095a72d
fa856d0
a7c017b
c0e4c76
d90a160
7db3894
2255be5
d6f58a6
1341f0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,54 +145,46 @@ facet_grid <- function(rows = NULL, cols = NULL, scales = "fixed", | |
} | ||
|
||
facets_list <- grid_as_facets_list(rows, cols) | ||
n <- length(facets_list) | ||
if (n > 2L) { | ||
stop("A grid facet specification can't have more than two dimensions", call. = FALSE) | ||
} | ||
if (n == 1L) { | ||
rows <- quos() | ||
cols <- facets_list[[1]] | ||
} else { | ||
rows <- facets_list[[1]] | ||
cols <- facets_list[[2]] | ||
} | ||
|
||
# Check for deprecated labellers | ||
labeller <- check_labeller(labeller) | ||
|
||
ggproto(NULL, FacetGrid, | ||
shrink = shrink, | ||
params = list(rows = rows, cols = cols, margins = margins, | ||
params = list(rows = facets_list$rows, cols = facets_list$cols, margins = margins, | ||
free = free, space_free = space_free, labeller = labeller, | ||
as.table = as.table, switch = switch, drop = drop) | ||
) | ||
} | ||
|
||
# returns a list containing exactly two quosures `rows` and `cols` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here it's not clear whether you're mentioning two single quosure or two quosure lists. I would use "quosure lists" or "lists of quosures" instead of just the plural "quosures". Also we generally capitalise comments. No full stop, unless there are more than one sentence. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Actually I'm struggling about the wording here...
I see. |
||
grid_as_facets_list <- function(rows, cols) { | ||
is_rows_vars <- is.null(rows) || rlang::is_quosures(rows) | ||
if (!is_rows_vars) { | ||
if (!is.null(cols)) { | ||
stop("`rows` must be `NULL` or a `vars()` list if `cols` is a `vars()` list", call. = FALSE) | ||
} | ||
return(as_facets_list(rows)) | ||
# For backward-compatibility | ||
facets_list <- as_facets_list(rows) | ||
if (length(facets_list) > 2L) { | ||
stop("A grid facet specification can't have more than two dimensions", call. = FALSE) | ||
} | ||
# fill with empty quosures | ||
facets <- list(rows = rlang::quos(), cols = rlang::quos()) | ||
facets[seq_along(facets_list)] <- facets_list | ||
# Do not compact the legacy specs | ||
return(facets) | ||
} | ||
|
||
is_cols_vars <- is.null(cols) || rlang::is_quosures(cols) | ||
if (!is_cols_vars) { | ||
stop("`cols` must be `NULL` or a `vars()` specification", call. = FALSE) | ||
} | ||
|
||
if (is.null(rows)) { | ||
rows <- quos() | ||
} else { | ||
rows <- rlang::quos_auto_name(rows) | ||
} | ||
if (is.null(cols)) { | ||
cols <- quos() | ||
} else { | ||
cols <- rlang::quos_auto_name(cols) | ||
} | ||
|
||
list(rows, cols) | ||
list( | ||
rows = compact_facets(as_facets_list(rows)), | ||
cols = compact_facets(as_facets_list(cols)) | ||
) | ||
} | ||
|
||
#' @rdname ggplot2-ggproto | ||
|
@@ -223,6 +215,10 @@ FacetGrid <- ggproto("FacetGrid", Facet, | |
base_cols <- combine_vars(data, params$plot_env, cols, drop = params$drop) | ||
base <- df.grid(base_rows, base_cols) | ||
|
||
if (nrow(base) == 0) { | ||
return(new_data_frame(list(PANEL = 1L, ROW = 1L, COL = 1L, SCALE_X = 1L, SCALE_Y = 1L))) | ||
} | ||
|
||
# Add margins | ||
base <- reshape2::add_margins(base, list(names(rows), names(cols)), params$margins) | ||
# Work around bug in reshape2 | ||
|
@@ -253,6 +249,11 @@ FacetGrid <- ggproto("FacetGrid", Facet, | |
cols <- params$cols | ||
vars <- c(names(rows), names(cols)) | ||
|
||
if (length(vars) == 0) { | ||
data$PANEL <- layout$PANEL | ||
return(data) | ||
} | ||
|
||
# Compute faceting values and add margins | ||
margin_vars <- list(intersect(names(rows), names(data)), | ||
intersect(names(cols), names(data))) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be
new_quosures()
. The casting form converts formulas to quosures etc, and at this point we should have standardised the inputs already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Thanks!