-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow geoms to ignore some set aesthetics #1585
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 also run into the same error when I try to run this example:
This does enforce the point that spelling mistakes and other minor errors will be easily caught and avoided. But this also breaks the packages like Animint, that rely on the use additional parameters (for e.g. I agree with @jiho in that we should be able to bypass this error by specifying a parameter so that we may only get a warning about additional params. It will ensure that the existing packages work with little modifications, all the while achieving your intended goal of not allowing user to make minor mistakes. I hope you give this a thought. |
Have you decided how to address this issue? Is there a PR? Or has the feature already been implemented? |
@hadley how do you want this? An |
I think there are two parts to the problem:
I don't like the idea of changing the error to a warning because it doesn't fix the underlying problem. |
or maybe |
another alternative would be to introduce a new parameter for storing any/all arbitrary layer-specific meta-data. for example the desired behavior would be that ggplot2 will recognize the |
I think the discussion has now diverged som much from the original (ignoring unknown aesthetics) that it warrants its own issue... |
Yes, let's move the extra parameters to a separate issue. It's not clear to me that you should be allowed to add extra parameters to a geom that you didn't create. |
Can't really understand the need either, but let's take that discussion in the new issue. @tdhock if you decide to pursue this please provide a thorough description of a use case. It is not clear from your code what |
@hadley are you dead-set on erroring out by default? Otherwise it could be solved with removing a couple of lines of code... |
I don't know if I'm dead-set against warnings, but an error certainly seems to better match the semantics of calling a function with an extra argument. I'd like to thoroughly explore alternatives before committing to a warning. |
I was more thinking about silently ignoring extra aesthetics. Agree that a warning is pretty useless. |
You mean using the list of aesthetics in |
I'm naively thinking about deleting line 102-106 in layer.r |
Oh you want to move completely back to the old experience where there's no warning or error if you mistype an aesthetic name? |
I think the distinction between parameter (i.e. a set aesthetic) or and aesthetic (i.e. a mapped aesthetic, inside an library("ggplot2")
ggplot(mtcars) + geom_point(aes(x=mpg, y=disp, sahpe=factor(cyl))) when in fact I want ggplot(mtcars) + geom_point(aes(x=mpg, y=disp, shape=factor(cyl))) Once this is solved, then the problem of passing additional information to geoms would arise for both set and mapped aesthetics. For this, I really think the way to go is a warning. You want to let the user know he/she added an argument that, maybe, won't do anything (e.g. adding So to take my initial example again: d <- data.frame(x=rnorm(10), y=rnorm(10), a=letters[1:10])
library("ggplot2")
my_plot <- function(data, mapping, ...) {
ggplot() +
geom_line(data=data, mapping=mapping, ...) +
geom_point(data=data, mapping=mapping, ...)
} I would expect both these lines: my_plot(d, aes(x=x, y=y, linetype=a))
my_plot(d, aes(x=x, y=y), linetype="dashed") to trigger a warning such as:
If you want to go out of your way to be helpful to the user, you might even make a partial match between the valid aesthetics and the provided ones to detect typos and provide helpful warnings such as library("ggplot2")
ggplot(mtcars) + geom_point(aes(x=mpg, y=disp, sahpe=factor(cyl)))
I see only one exception: inherited aesthetics. You don't want ggplot(mtcars, aes(x=mpg, y=disp, shape=factor(cyl)) + geom_point() + geom_path() to emit a warning about So basically, either of these solutions would be fine with me:
|
I'll let it be up to you @hadley, one of the bullets is definitely easier ;-) |
I agree that aesthetics should be treated identically regardless of whether or not they're set or mapped. Silently ignoring unknown aesthetics is definitely bad (regardless of whether or not that's standard behaviour for functions that use It would be nice to automatically suggest correct names using I worry about emitting warnings rather than throwing errors because it means that you'd expect functions like |
I may have a stab at it if I can find the time (doing field work now, with a pretty crazy schedule, so not sure when). re- the warning vs. error situation. I suggested a warning because, in a function like |
In an ideal world, ggplot2 would have throw errors to avoid these problems from the get go. But it seems like throwing errors now is a bit too harsh — I don't think warnings are as good, but they're a reasonable compromise given code in the wild. I'll have a go at making the change. |
That's great, thanks! |
Let us say I want to combine a few geoms to make a specific plot, I can define such a function:
But then, if I want to change the linetype,
geom_point
throws an error becauselinetype
is meaningless for it:If I recall correctly, this was just silently ignored in the past. Interestingly, mapped aesthetics do not throw this error:
But the result is pretty useless of course ;-). A more useful example would be
Still moving the text up is not possible after the fact, with something like:
I understand the rationale in telling the user explicitly about useless aesthetics, but:
suppressWarnings
and that would help).PS: The rationale for this comment is that I often combine geoms in
autoplot
but want to retain the flexibility of mapping/setting aesthetics from the higher level function.The text was updated successfully, but these errors were encountered: