Skip to content
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

Enable 2D structures as aesthetics #6076

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* (internal) removed barriers for using 2D structures as aesthetics
(@teunbrand, #4189).
* The `summary()` method for ggplots is now more terse about facets
(@teunbrand, #5989).
* `guide_bins()`, `guide_colourbar()` and `guide_coloursteps()` gain an `angle`
Expand Down
2 changes: 1 addition & 1 deletion R/geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ NULL
.stroke <- 96 / 25.4

check_aesthetics <- function(x, n) {
ns <- lengths(x)
ns <- list_sizes(x)
good <- ns == 1L | ns == n

if (all(good)) {
Expand Down
8 changes: 4 additions & 4 deletions R/layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
#' `NA`, the default, includes if any aesthetics are mapped.
#' `FALSE` never includes, and `TRUE` always includes.
#' It can also be a named logical vector to finely select the aesthetics to
#' display. To include legend keys for all levels, even
#' when no data exists, use `TRUE`. If `NA`, all levels are shown in legend,
#' display. To include legend keys for all levels, even
#' when no data exists, use `TRUE`. If `NA`, all levels are shown in legend,
#' but unobserved levels are omitted.
#' @param inherit.aes If `FALSE`, overrides the default aesthetics,
#' rather than combining with them. This is most useful for helper functions
Expand Down Expand Up @@ -326,7 +326,7 @@ Layer <- ggproto("Layer", NULL,
}

n <- nrow(data)
aes_n <- lengths(evaled)
aes_n <- list_sizes(evaled)
if (n == 0) {
# No data, so look at longest evaluated aesthetic
if (length(evaled) == 0) {
Expand All @@ -351,7 +351,7 @@ Layer <- ggproto("Layer", NULL,
} else {
evaled$PANEL <- data$PANEL
}
evaled <- lapply(evaled, unname)
evaled <- lapply(evaled, vec_set_names, names = NULL)
evaled <- as_gg_data_frame(evaled)
evaled <- add_group(evaled)
evaled
Expand Down
2 changes: 1 addition & 1 deletion R/layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ scale_apply <- function(data, vars, method, scale_id, scales) {

lapply(vars, function(var) {
pieces <- lapply(seq_along(scales), function(i) {
scales[[i]][[method]](data[[var]][scale_index[[i]]])
scales[[i]][[method]](vec_slice(data[[var]], scale_index[[i]]))
})
# Remove empty vectors to avoid coercion issues with vctrs
pieces[lengths(pieces) == 0] <- NULL
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,20 @@ test_that("layer_data returns a data.frame", {
l <- geom_point(data = nrow)
expect_snapshot_error(l$layer_data(mtcars))
})

test_that("data.frames and matrix aesthetics survive the build stage", {
df <- data_frame0(
x = 1:2,
g = matrix(1:4, 2),
f = data_frame0(a = 1:2, b = c("c", "d"))
)

p <- layer_data(
ggplot(df, aes(x, x, colour = g, shape = f)) +
geom_point() +
scale_colour_identity() +
scale_shape_identity()
)
expect_vector(p$colour, matrix(NA_integer_, nrow = 0, ncol = 2), size = 2)
expect_vector(p$shape, data_frame0(a = integer(), b = character()), size = 2)
})
Loading