Skip to content

Nonexistent color names: data not displayed without throwing warning/error #3644

Closed

Description

@damianooldoni

I would expect an error or at least a warning if color names are not correct. Now the data linked to such colors are not displayed.
I suggest using a warning which informs the user that his/her color mapping has been ignored and default color scale is used.

Here a minimal reprex using the standard example in documentation:

p <- ggplot(mtcars, aes(mpg, wt)) +
+   geom_point(aes(colour = factor(cyl)))
p + scale_colour_manual(values = c("darkred", "blue", "dark green")) #dark green instead of darkgreen

Activity

  1. yutannihilation commented on Dec 19, 2019

    @yutannihilation
    Member

    @thomasp85
    Though I don't have a closer look at this yet, it seems scales package unintendedly became tolerant for invalid colour names after it switched to farver by r-lib/scales#224 mainly because of the difference below. Is it possible to add some option to decode_colour() to raise error when the colour name is invalid?

    farver::decode_colour("foo")
    #>       r  g  b
    #> [1,] NA NA NA
    grDevices::col2rgb("foo")
    #> Error in grDevices::col2rgb("foo"): invalid color name 'foo'

    Created on 2019-12-19 by the reprex package (v0.3.0)

    ...btw, this particular example gives us another surprise. Wow...

    farver::decode_colour("dark green")
    #>       r  g  b
    #> [1,] NA NA NA
    grDevices::col2rgb("darkgreen")
    #>       [,1]
    #> red      0
    #> green  100
    #> blue     0
    grDevices::col2rgb("dark green")
    #>       [,1]
    #> red      0
    #> green  100
    #> blue     0

    Created on 2019-12-19 by the reprex package (v0.3.0)

  2. thomasp85 commented on Dec 19, 2019

    @thomasp85
    Member

    🤬 the ability to accept any number of colour name formats in col2rgb() is just plain annoying...

    I'm not sure that throwing an error in farver is the best way forward though? @hadley do you have any preferences

  3. thomasp85 commented on Dec 19, 2019

    @thomasp85
    Member

    I can get behind throwing a warning

  4. yutannihilation commented on Dec 19, 2019

    @yutannihilation
    Member

    Yeah, I agree the behavior of col2rgb() is not fine. A warning sounds good.

  5. hadley commented on Dec 19, 2019

    @hadley
    Member

    Warning seems reasonable to me (although I'd also be fine with an error)

  6. yutannihilation commented on Dec 20, 2019

    @yutannihilation
    Member

    It seems this is fixed by thomasp85/farver@0360a08. Thanks @thomasp85!

    library(ggplot2)
    
    p <- ggplot(mtcars, aes(mpg, wt)) +
      geom_point(aes(colour = factor(cyl)))
    
    # dark green is accepted now
    p + scale_colour_manual(values = c("darkred", "blue", "dark green")) 

    # bad names are now warned
    p + scale_colour_manual(values = c("darkred", "blue", "foo")) 
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo
    
    #> Warning: Unknown colour name: foo

    warnings()

    Created on 2019-12-20 by the reprex package (v0.3.0)

  7. hadley commented on Dec 20, 2019

    @hadley
    Member

    @thomasp85 I think the 14 warnings here suggest an error would be better

  8. thomasp85 commented on Dec 20, 2019

    @thomasp85
    Member

    Yeah - maybe you’re right

  9. lock commented on Jun 24, 2020

    @lock

    This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

  10. locked and limited conversation to collaborators on Jun 24, 2020
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions