Skip to content

ggplot throws an error when a data is zero row and the lengths of aesthetics are not zero #2850

Closed
@hisakatha

Description

@hisakatha

If a data is zero row and the lengths of aesthetics are not zero, ggplot throws an error with an wrong length of the data as follows.

Error: Aesthetics must be either length 1 or the same as the data (1): x, y, colour

I think the number in the parentheses indicates the length of the data, but it is wrong, since the length is zero.

In addition, this behavior doesn't get along with constant (length-one) aesthetics, while they can be combined with non-zero-row data. I hope ggplot tolerates combination of zero-row data and constant aesthetics.

Example (using R 3.5.1 and ggplot2 3.0.0):

library(ggplot2)
d1 <- data.frame(xval = rep(1:5, 4), yval = 1:20)

# The followings are OK
ggplot(d1, aes(xval, yval))
ggplot(d1, aes(xval, yval, colour = "Type 1"))
nrow(d1[0,])
#> [1] 0
ggplot(d1[0,], aes(xval, yval))

# This causes an error
ggplot(d1[0,], aes(xval, yval, colour = "Type 1"))
#> Error: Aesthetics must be either length 1 or the same as the data (1): x, y, colour

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorinternals 🔎

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions