Skip to content

Suggestion: Point out which continuous/discrete value was wrongly supplied to which discrete/continuous scale #4258

Closed

Description

@jgigla

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.

Activity

  1. clauswilke commented on Nov 4, 2020

    @clauswilke
    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().

  2. iagogv3 commented on Nov 19, 2020

    @iagogv3

    @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?

  3. clauswilke commented on Nov 19, 2020

    @clauswilke
    Member

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

  4. eliocamp commented on Jan 12, 2021

    @eliocamp
    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)

  5. added
    messages
    on Mar 24, 2021
  6. teunbrand commented on Feb 14, 2023

    @teunbrand
    Collaborator

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

  7. added a commit that references this issue on Jul 4, 2023
    66c153a
  8. added a commit that references this issue on Sep 12, 2023
    5da2d30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    messagesscales 🐍

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions