-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Layer parameter controlling facet layout #6336
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
Changes from 8 commits
1bdf948
28ba951
c0ba01c
8d9d955
f7feb8c
7ae5358
b9edb48
b3bb675
549eba9
24f95ae
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 |
---|---|---|
|
@@ -88,7 +88,95 @@ Facet <- ggproto("Facet", NULL, | |
cli::cli_abort("Not implemented.") | ||
}, | ||
map_data = function(data, layout, params) { | ||
cli::cli_abort("Not implemented.") | ||
|
||
if (empty(data)) { | ||
return(vec_cbind(data %|W|% NULL, PANEL = integer(0))) | ||
} | ||
|
||
vars <- params$facet %||% c(params$rows, params$cols) | ||
|
||
if (length(vars) == 0) { | ||
data$PANEL <- layout$PANEL | ||
return(data) | ||
} | ||
|
||
grid_layout <- all(c("rows", "cols") %in% names(params)) | ||
layer_layout <- attr(data, "layout") | ||
if (identical(layer_layout, "fixed")) { | ||
n <- vec_size(data) | ||
data <- vec_rep(data, vec_size(layout)) | ||
data$PANEL <- vec_rep_each(layout$PANEL, n) | ||
return(data) | ||
} | ||
|
||
# Compute faceting values | ||
facet_vals <- eval_facets(vars, data, params$.possible_columns) | ||
|
||
include_margins <- !isFALSE(params$margin %||% FALSE) && | ||
nrow(facet_vals) == nrow(data) && grid_layout | ||
if (include_margins) { | ||
# Margins are computed on evaluated faceting values (#1864). | ||
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. I merged the wrap/grid layout mapping because it reduces duplication and the only tangible difference was dealing with margins. Because this makes the diff harder to read, I've put comments at which parts weren't in here before. |
||
facet_vals <- reshape_add_margins( | ||
vec_cbind(facet_vals, .index = seq_len(nrow(facet_vals))), | ||
list(intersect(names(params$rows), names(facet_vals)), | ||
intersect(names(params$cols), names(facet_vals))), | ||
params$margins %||% FALSE | ||
) | ||
# Apply recycling on original data to fit margins | ||
# We're using base subsetting here because `data` might have a superclass | ||
# that isn't handled well by vctrs::vec_slice | ||
data <- data[facet_vals$.index, , drop = FALSE] | ||
facet_vals$.index <- NULL | ||
} | ||
|
||
# If we need to fix rows or columns, we make the corresponding faceting | ||
# variables missing on purpose | ||
if (grid_layout) { | ||
if (identical(layer_layout, "fixed_rows")) { | ||
facet_vals <- facet_vals[setdiff(names(facet_vals), names(params$cols))] | ||
} | ||
if (identical(layer_layout, "fixed_cols")) { | ||
facet_vals <- facet_vals[setdiff(names(facet_vals), names(params$rows))] | ||
} | ||
} | ||
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. This part is new |
||
|
||
# If any faceting variables are missing, add them in by | ||
# duplicating the data | ||
missing_facets <- setdiff(names(vars), names(facet_vals)) | ||
if (length(missing_facets) > 0) { | ||
|
||
to_add <- unique0(layout[missing_facets]) | ||
|
||
data_rep <- rep.int(seq_len(nrow(data)), nrow(to_add)) | ||
facet_rep <- rep(seq_len(nrow(to_add)), each = nrow(data)) | ||
|
||
data <- unrowname(data[data_rep, , drop = FALSE]) | ||
facet_vals <- unrowname(vec_cbind( | ||
unrowname(facet_vals[data_rep, , drop = FALSE]), | ||
unrowname(to_add[facet_rep, , drop = FALSE]) | ||
)) | ||
} | ||
|
||
if (nrow(facet_vals) < 1) { | ||
# Add PANEL variable | ||
data$PANEL <- NO_PANEL | ||
return(data) | ||
} | ||
|
||
facet_vals[] <- lapply(facet_vals, as_unordered_factor) | ||
facet_vals[] <- lapply(facet_vals, addNA, ifany = TRUE) | ||
layout[] <- lapply(layout, as_unordered_factor) | ||
|
||
# Add PANEL variable | ||
keys <- join_keys(facet_vals, layout, by = names(vars)) | ||
data$PANEL <- layout$PANEL[match(keys$x, keys$y)] | ||
|
||
# Filter panels when layer_layout is an integer | ||
if (is_integerish(layer_layout)) { | ||
data <- vec_slice(data, data$PANEL %in% layer_layout) | ||
} | ||
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. This part is new |
||
|
||
data | ||
}, | ||
init_scales = function(layout, x_scale = NULL, y_scale = NULL, params) { | ||
scales <- list() | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
This is new