rstudio / thematic

Theme ggplot2, lattice, and base graphics based on a few simple settings.
https://rstudio.github.io/thematic/
Other
244 stars 10 forks source link

`sequential_gradient` appears to truncate color space #61

Open dgkf opened 4 years ago

dgkf commented 4 years ago

Describe the problem

Thanks for the wonderful talk today at R/Pharma, @cpsievert! I've been having fun playing around with thematic today. In the course of tinkering with the package and trying to build some of my own themes, I found that the values produced by the generated function of sequential_gradient were starting after the expected starting color value.

library(thematic)
grad_func <- thematic::sequential_gradient(1.0, 1.0, n = 5)
grad_func(fg = "#FF0000", accent = "#00FF00", bg = "#0000FF")
# [1] "#DD8D00" "#95D900" "#63D95F" "#7B80B6" "#0000FF"

I would expect the first value to be the same as the provided fg color, but it seems to begin the gradient with a value already partway in transition from fg to accent.

Session Info


─ Session info ─────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.0.2 (2020-06-22)
 os       macOS Catalina 10.15.7      
 system   x86_64, darwin18.7.0        
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/Los_Angeles         
 date     2020-10-14                  

─ Packages ─────────────────────────────────────────────────────────────────────────────────
 package     * version     date       lib source                           
 assertthat    0.2.1       2019-03-21 [1] CRAN (R 4.0.2)                   
 backports     1.1.10      2020-09-15 [1] CRAN (R 4.0.2)                   
 callr         3.5.1       2020-10-13 [1] CRAN (R 4.0.2)                   
 cli           2.1.0       2020-10-12 [1] CRAN (R 4.0.2)                   
 colorspace    1.4-1       2019-03-18 [1] CRAN (R 4.0.2)                   
 crayon        1.3.4       2017-09-16 [1] CRAN (R 4.0.2)                   
 desc          1.2.0       2018-05-01 [1] CRAN (R 4.0.2)                   
 devtools      2.3.1       2020-07-21 [1] CRAN (R 4.0.2)                   
 digest        0.6.25      2020-02-23 [1] CRAN (R 4.0.2)                   
 dplyr         1.0.2       2020-08-18 [1] CRAN (R 4.0.2)                   
 ellipsis      0.3.1       2020-05-15 [1] CRAN (R 4.0.2)                   
 fansi         0.4.1       2020-01-08 [1] CRAN (R 4.0.2)                   
 fs            1.5.0       2020-07-31 [1] CRAN (R 4.0.2)                   
 generics      0.0.2       2018-11-29 [1] CRAN (R 4.0.2)                   
 ggplot2       3.3.2       2020-06-19 [1] CRAN (R 4.0.2)                   
 glue          1.4.2       2020-08-27 [1] CRAN (R 4.0.2)                   
 gtable        0.3.0       2019-03-25 [1] CRAN (R 4.0.2)                   
 knitr         1.30        2020-09-22 [1] CRAN (R 4.0.2)                   
 lifecycle     0.2.0       2020-03-06 [1] CRAN (R 4.0.2)                   
 magrittr      1.5         2014-11-22 [1] CRAN (R 4.0.2)                   
 memoise       1.1.0       2017-04-21 [1] CRAN (R 4.0.2)                   
 munsell       0.5.0       2018-06-12 [1] CRAN (R 4.0.2)                   
 pillar        1.4.6       2020-07-10 [1] CRAN (R 4.0.2)                   
 pkgbuild      1.1.0       2020-07-13 [1] CRAN (R 4.0.2)                   
 pkgconfig     2.0.3       2019-09-22 [1] CRAN (R 4.0.2)                   
 pkgload       1.1.0       2020-05-29 [1] CRAN (R 4.0.2)                   
 prettyunits   1.1.1       2020-01-24 [1] CRAN (R 4.0.2)                   
 processx      3.4.4       2020-09-03 [1] CRAN (R 4.0.2)                   
 ps            1.4.0       2020-10-07 [1] CRAN (R 4.0.2)                   
 purrr         0.3.4       2020-04-17 [1] CRAN (R 4.0.2)                   
 R6            2.4.1       2019-11-12 [1] CRAN (R 4.0.2)                   
 remotes       2.2.0       2020-07-21 [1] CRAN (R 4.0.2)                   
 rlang         0.4.8       2020-10-08 [1] CRAN (R 4.0.2)                   
 rprojroot     1.3-2       2018-01-03 [1] CRAN (R 4.0.2)                   
 rstudioapi    0.11        2020-02-07 [1] CRAN (R 4.0.2)                   
 scales        1.1.1       2020-05-11 [1] CRAN (R 4.0.2)                   
 sessioninfo   1.1.1       2018-11-05 [1] CRAN (R 4.0.2)                   
 testthat      2.99.0.9000 2020-10-14 [1] Github (r-lib/testthat@12a39de)  
 thematic      0.1.0.9000  2020-10-14 [1] Github (rstudio/thematic@95927f4)
 tibble        3.0.4       2020-10-12 [1] CRAN (R 4.0.2)                   
 tidyselect    1.1.0       2020-05-11 [1] CRAN (R 4.0.2)                   
 usethis       1.6.1       2020-04-29 [1] CRAN (R 4.0.2)                   
 vctrs         0.3.4       2020-08-29 [1] CRAN (R 4.0.2)                   
 withr         2.3.0       2020-09-22 [1] CRAN (R 4.0.2)                   
 xfun          0.17        2020-09-09 [1] CRAN (R 4.0.2)                   

[1] /usr/local/Cellar/r/4.0.2_1/lib/R/library
cpsievert commented 4 years ago

Thanks @dgkf!

This is somewhat by design -- sequential_gradient() also takes into account the perceptual distance between fg/accent and bg/accent and weights accordingly (in this case, there's a larger perceptual distance between bg and accent, which is why you get the bg endpoint, but not the fg).

As long as fg/accent/bg are based on the same or similar hue (which they should be in order for it even to make sense to make sequential colorscale out these colors), then I don't think this is much of problem (feel free to convince me otherwise :)

Also, keep in mind that you can also provide the colors directly to sequential, in which cases, the "usual" linear interpolation is applied:

thematic_on(sequential = c("#FF0000", "#00FF00", "#0000FF"))
qplot(1:10, 1:10, color = 1:10)

Also, if you want to do this in a way that works with auto-theming, you can provide a function:

thematic_on(sequential = function(fg, accent, bg) { scales::colour_ramp(c(fg, accent, bg))(seq(0, 1, by = 0.1)) })
dgkf commented 3 years ago

Thanks @cpsievert - that sounds good. I didn't realize it was doing additional work to try to make the color presentation more consistent. Perhaps a mention of this behavior could be added to the sequential_gradient documentation.

I should have tried out just passing values - I think that works just fine for my purposes.