Skip to content

Commit a7b3135

Browse files
lionel-hadley
authored andcommitted
Allow column vector inputs (#2635)
Fixes #2609
1 parent 54de616 commit a7b3135

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

NEWS.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
(#2610).
2121

2222
* Error: Column `y` must be a 1d atomic vector or a list
23-
24-
Internally, ggplot2 now uses `as.data.frame(tibble::as_tibble(x))` to
25-
convert a list into a data frame. This improves ggplot2's support for
23+
24+
Internally, ggplot2 now uses `as.data.frame(tibble::as_tibble(x))` to
25+
convert a list into a data frame. This improves ggplot2's support for
2626
list-columns (needed for sf support), at a small cost: you can no longer
27-
use matrix-columns. These are rarely used but are produced by `scale()`;
28-
to continue to use `scale()` you'll need to wrap it with `as.numeric()`,
29-
e.g. `as.numeric(scale(x))`.
30-
27+
use matrix-columns. Note that unlike tibble we still allow column vectors
28+
such as returned by `base::scale()` because of their widespread use.
29+
3130
* Error: More than one expression parsed
3231

3332
Previously `aes_string(x = c("a", "b", "c"))` silently returned

R/geom-.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Geom <- ggproto("Geom",
116116
missing_eval <- compact(missing_eval)
117117

118118
if (empty(data)) {
119-
data <- as.data.frame(tibble::as_tibble(missing_eval))
119+
data <- as_gg_data_frame(missing_eval)
120120
} else {
121121
data[names(missing_eval)] <- missing_eval
122122
}

R/layer.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ Layer <- ggproto("Layer", NULL,
235235
evaled$PANEL <- data$PANEL
236236
}
237237
evaled <- lapply(evaled, unname)
238-
evaled <- as.data.frame(tibble::as_tibble(evaled))
238+
evaled <- as_gg_data_frame(evaled)
239239
evaled <- add_group(evaled)
240240
evaled
241241
},

R/utilities.r

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,19 @@ seq_asc <- function(to, from) {
414414
# Needed to trigger package loading
415415
#' @importFrom tibble tibble
416416
NULL
417+
418+
# Check inputs with tibble but allow column vectors (see #2609 and #2374)
419+
as_gg_data_frame <- function(x) {
420+
x <- lapply(x, validate_column_vec)
421+
as.data.frame(tibble::as_tibble(x))
422+
}
423+
validate_column_vec <- function(x) {
424+
if (is_column_vec(x)) {
425+
dim(x) <- NULL
426+
}
427+
x
428+
}
429+
is_column_vec <- function(x) {
430+
dims <- dim(x)
431+
length(dims) == 2L && dims[[2]] == 1L
432+
}

tests/testthat/test-layer.r

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ test_that("unknown NULL asthetic doesn't create warning (#1909)", {
2020
expect_warning(geom_point(aes(blah = NULL)), NA)
2121
})
2222

23+
test_that("column vectors are allowed (#2609)", {
24+
df <- data.frame(x = 1:10)
25+
df$y <- scale(1:10) # Returns a column vector
26+
p <- ggplot(df, aes(x, y))
27+
expect_is(layer_data(p), "data.frame")
28+
})
29+
2330
# Data extraction ---------------------------------------------------------
2431

2532
test_that("layer_data returns a data.frame", {

0 commit comments

Comments
 (0)