-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Comments
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 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) |
@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)) |
These are the relevant lines: Lines 261 to 270 in cabb746
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. |
Ah, I see. I would still say that the error message is confusing: If 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. |
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 |
Thank you for keeping me updated. That decision makes sense to me. |
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:
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.
The text was updated successfully, but these errors were encountered: