-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Reference lines break facet_wrap when facets are the product of a function #2963
Comments
Adding a reprex. It's helpful to have the code as well as the output with error messages. The error message is, in effect, the same, that the first object mentioned in the operation (in this case library(ggplot2)
library(dplyr)
## Example 1 ------------------------------
## Works
ggplot(mtcars, aes(mpg, cyl)) +
geom_point() +
geom_vline(xintercept = 20) +
facet_wrap(vars(am)) ## Doesn't work
ggplot(mtcars, aes(mpg, cyl)) +
geom_point() +
geom_vline(xintercept = 20) +
facet_wrap(vars(2 * am)) # <-- there's an operation involved
#> Error in rlang::eval_tidy(facet, data, env): object 'am' not found
## Works without `geom_vline()`
ggplot(mtcars, aes(mpg, cyl)) +
geom_point() +
facet_wrap(vars(2 * am)) Created on 2018-10-25 by the reprex package (v0.2.1.9000) |
I didn't know the reprex package. It's very cool! I can't use it for images, though, because my work firewall has banned imgur... |
This is also the case when using tidyeval in library(ggplot2)
ggplot(mtcars, aes(mpg, cyl)) +
geom_point() +
geom_vline(xintercept = 20) +
facet_wrap(vars(.data$am))
#> Column `am` not found in `.data` |
Leaving this here to ponder...I don't know if there's an existing way to inspect a quosure to find which objects in Lines 424 to 437 in 4ce3953
|
Hi, I had the same problem and I solved it through adding its own aesthetic to the geom_vline: I can't explain why, though, as the current help file for geom_vline does not mention it. I only tried it because I found |
A quick note. gghighlight had a similar issue and fixed in yutannihilation/gghighlight#78 by ignoring the failed evaluations (and stops only when the all calculations fail). I think the fix would be the same here because the problem is not whether the symbols in the expression refer to some data but whether the expression can be successfully evaluated (e.g. |
It's been a long time since I looked into this, but I did at one point write code that figures out which variables from a quosure are referred to. It's been too long since I looked at the facet code to know if something like that is needed or whether an evaluation check would work, but I do think this should be fixed, as |
Nice work, thanks. I think your code covers most of the cases. Still, I concern
If |
I'm not either...these are all good points. The static analysis is more complicated than I would like for this problem. We could special case |
If I understand correctly, the general problem is that library(rlang)
eval_default <- function(x, data, default) {
force(default)
suppressWarnings(
tryCatch(
error = function(e) default,
eval_tidy(x, data)
)
)
}
data <- data.frame(x = 1)
eval_default(quo(x), data, NULL)
#> [1] 1
eval_default(quo(y), data, NULL)
#> NULL Created on 2020-01-12 by the reprex package (v0.3.0) What I found strange is that it needed the In any case, I wonder whether something like this |
@paleolimbot @clauswilke |
Yes, I'll file an issue. I remember the warning now. It was "restarting interrupted promise evaluation." That seemed sufficiently scary that I didn't just want to ignore it, but there doesn't seem to be any actual problem associated with this warning. |
Issue submitted: r-lib/rlang#888 |
Thanks! |
Thank you all for your hard work! I honestly had no idea that the rabbit hole would go this deep. I'm out of my depth but you guys seem to be on top of things :) |
I want to add some reference lines to a faceted plot. The lines should be the same in every facet. I want to use a custom value not mapped from the data. It works just fine as long as I use plain variables for faceting, but when I mix or alter them, I get an error, which depends on the operation I perform.
I think the problem lies in the fact that the plot wants to compute facets with for the non-mapped layer, but the data doesn't exist. I don't think geom_line has a problem, but rather facet_wrap.
The text was updated successfully, but these errors were encountered: