-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Suggestion: Point out which continuous/discrete value was wrongly supplied to which discrete/continuous scale #4258
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 agree this would be nice. It's not that easy to implement, though. The error is generated by Line 17 in 1c09bae
We don't have any of the relevant info available when this call is made. All we have is the range of the scale and the data values. To improve the error message, we would have to add the auxiliary debugging info to the |
@clauswilke is the behaviour you tell -"the error generated by |
Yes, it looks like #4241 is the same problem. |
Would something like this work? library(ggplot2)
df <- data.frame(x = rnorm(6) * 100,
y = factor(c(100, 100, 101, 101, 102, 102)),
col = rnorm(6))
ggplot(df, aes(x, y, fill = col)) +
geom_point() +
scale_fill_viridis_d()
#> Error: Continuous value supplied to discrete scale: viridis_d The necessary changes can be seen here. The gist of it is that I'm adding if (inherits(range, "try-error")) {
stop("Continuous value supplied to discrete scale: ", self$scale_name,
call. = FALSE)
} A better solution would probably involve changing the error message in Created on 2021-01-12 by the reprex package (v0.3.0) |
FWIW #5086 would make Elio's suggestion unworkable. It might be best to add a scale_name/aesthetic field to the Range class. |
* More informative calls in constructors * Add test for scale calls * Fix call for feed-forward scales * Fix calls for gnarly default scales * Fix calls for transformation scales * Reoxygenate * Supply `call` to messages * Accept periods at ends of messages * Forward calls to checkers * Fix #4258 * `find_scale()` generates call * `xlim`/`ylim` have appropriate calls * `check_transformation()` throws more informative warning * Remove orphaned code * Deprecate `scale_name` argument * Purge `scale_name` * Test deprecation messages * Add NEWS bullet * conditionally test time scales
When working with very complex plots, with many different values, mappings, and scales, it often happens that I mix something up and accidentally supply a continuous value to a discrete scale, or vice versa. In this case, while ggplot does tell me which way the mix-up occurred, it does not tell me which value was specifically wrongly supplied to which scale. Instead, I have to figure this out myself, often by frustrating trial-and-error if it is not immediately obvious from the code.
I would love it if the error message included:
Here is some reproducible code to illustrate my problem:
When I run this, I get the dreaded:
However, finding out which scale would be changed—in this case, to be continuous—requires reading and understanding the code. (Of course, in this case, it is the
fill
scale, but can you see this at first glance?) It is even harder to do if:ggnewscale
)ggplot2
and don't even know what the error message is trying to tell you in the first place...Now my simple suggestion would be to amend the error message, so it reads for example:
I'm sure the exact wording can be improved; this is just an idea. I don't know how much effort this would be to implement, but it would certainly improve my quality of life a lot, and perhaps even that of others.
The text was updated successfully, but these errors were encountered: