Skip to content

Misleading error message when plotting with empty data.frame #4588

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

Closed
jarauh opened this issue Aug 18, 2021 · 6 comments · Fixed by #4670
Closed

Misleading error message when plotting with empty data.frame #4588

jarauh opened this issue Aug 18, 2021 · 6 comments · Fixed by #4670
Labels
bug an unexpected problem or unintended behavior

Comments

@jarauh
Copy link

jarauh commented Aug 18, 2021

When plotting with an empty data (which may happen when filtering goes wrong), there is an error if constant aesthetics are mixed with column names, and the error message is a little confusing.

Example:

> ggplot(head(mtcars, 0)) + geom_point(aes(x = hp, y = mpg)) # no error here, plot is empty
> ggplot(head(mtcars, 0)) + geom_point(aes(x = 3, y = 4))        # no error here, plot contains one point
> ggplot(head(mtcars, 0)) + geom_point(aes(x = 3, y = mpg))   # error!
 Fehler: Aesthetics must be either length 1 or the same as the data (1): y
Run `rlang::last_error()` to see where the error occurred. 

Taken literally, the error message is wrong: Aesthetics y does have the same length as the data (0).

Expected behaviour: No error message, return an empty plot.

@karawoo
Copy link
Member

karawoo commented Aug 18, 2021

I think an error makes sense here because unlike the first two plots, the x and y aesthetics have different lengths (1 vs. 0). The value you provided to x is the data with length 1 the message is referencing. As another example:

library("ggplot2")
ggplot() + geom_point(aes(x = 3, y = numeric(0)))
#> Error: Aesthetics must be either length 1 or the same as the data (1): y

Created on 2021-08-18 by the reprex package (v1.0.0)

@jarauh
Copy link
Author

jarauh commented Aug 19, 2021

@karawoo aesthetics of different length are usually ok, as long as one of the lengths is 1. Example:

ggplot() + geom_point(aes(x = 3, y = 1:3))

@karawoo
Copy link
Member

karawoo commented Aug 19, 2021

These are the relevant lines:

ggplot2/R/layer.r

Lines 261 to 270 in cabb746

n <- nrow(data)
if (n == 0) {
# No data, so look at longest evaluated aesthetic
if (length(evaled) == 0) {
n <- 0
} else {
n <- max(vapply(evaled, length, integer(1)))
}
}
check_aesthetics(evaled, n)

Aesthetics of different length are ok as long as they are all length 1 or the length of the data; in my example (and your third example) the length of the data is taken to be 1 because that is the longest evaluated aesthetic, so the y aesthetic can't be length 0.

@jarauh
Copy link
Author

jarauh commented Aug 20, 2021

Ah, I see. I would still say that the error message is confusing: If nrow(data) == 0, then 'n' is not the "length of the data."

In my opinion, the error message should be changed.

I'm also thinking whether it makes sense to change the behaviour and not raise an error but return an empty plot? In my use case that would be the right thing to do (or at least a decent thing to do), but there may be cases where that would be even more confusing.

@thomasp85
Copy link
Member

It has taken some time to settle on how the recycling rules in the tidyverse should be defined, but the consensus now is that 1 length vectors should be recycled to whatever the length of the other vector is (including 0)... However, ggplot2 still needs to be updated to reflect this

@thomasp85 thomasp85 added the bug an unexpected problem or unintended behavior label Oct 28, 2021
@jarauh
Copy link
Author

jarauh commented Oct 28, 2021

Thank you for keeping me updated. That decision makes sense to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants