Skip to content

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

Closed
jgigla opened this issue Nov 4, 2020 · 5 comments · Fixed by #5343
Labels
messages requests for improvements to error, warning, or feedback messages scales 🐍

Comments

@jgigla
Copy link

jgigla commented Nov 4, 2020

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:

  • the name of the scale which a continuous/discrete value was wrongly supplied to,
  • the aesthetic which is mapped to this scale,
  • and the value which is mapped to this aesthetic.
  • Possibly even more useful information that is available internally, that I am not aware of?

Here is some reproducible code to illustrate my problem:

library(tidyverse)

set.seed(0)

df = tibble(col1 = rnorm(6) * 100,
            col2 = factor(c(100, 100, 101, 101, 102, 102)),
            col3 = rnorm(6),
            col4 = factor(c(1, 2, 1, 3, 5, 2)))

df %>% ggplot(mapping = aes(x = col1, y = col2, fill = col3, colour = col4)) +
  geom_point(pch = 21, size = 10, stroke = 3) +
  scale_fill_viridis_d(option = "viridis") +
  scale_colour_viridis_d(option = "magma")

When I run this, I get the dreaded:

Error: Continuous value supplied to discrete scale

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:

  • the plot is assembled at distant code locations; for example when a "barebones" plot only with geoms is constructed, and then "finished off" with scales/labs/etc. further down in the script; especially if the same plot can be "finished off" in multiple different ways
  • the same aesthetic has multiple values mapped to multiple scales (using something like ggnewscale)
  • there are scales involved where it is not immediately obvious whether they are discrete or continuous, particularly if they are supplied by another external module
  • binned scales are involved
  • you are new to ggplot2 and don't even know what the error message is trying to tell you in the first place...
  • and so on

Now my simple suggestion would be to amend the error message, so it reads for example:

Error: Continuous value (col3) supplied to discrete scale ("viridis") for aesthetic "fill"

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.

@clauswilke
Copy link
Member

I agree this would be nice. It's not that easy to implement, though. The error is generated by scales::train_discrete(), which is called here:

self$range <- scales::train_discrete(x, self$range, drop = drop, na.rm = na.rm)

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 Range object and then pass that info to the call to scales::train_discrete().

@iagogv3
Copy link

iagogv3 commented Nov 19, 2020

@clauswilke is the behaviour you tell -"the error generated by scales::..."- similar to the one which happens in issue #4241, so it would apply a similar conclusion on adding auxiliary debugging?

@clauswilke
Copy link
Member

Yes, it looks like #4241 is the same problem.

@eliocamp
Copy link
Contributor

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 scale_name as a parameter to discrete_range() and then use that information if there's an error. Now, this right here is not ideal:

    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 scales::train_discrete() .

Created on 2021-01-12 by the reprex package (v0.3.0)

@thomasp85 thomasp85 added messages requests for improvements to error, warning, or feedback messages scales 🐍 labels Mar 24, 2021
@teunbrand
Copy link
Collaborator

FWIW #5086 would make Elio's suggestion unworkable. It might be best to add a scale_name/aesthetic field to the Range class.

teunbrand added a commit to teunbrand/ggplot2 that referenced this issue Jul 4, 2023
teunbrand added a commit that referenced this issue Sep 12, 2023
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
messages requests for improvements to error, warning, or feedback messages scales 🐍
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants