kwstat / pals

Color Palettes and Palette Evaluation Tools
https://kwstat.github.io/pals/
Other
81 stars 4 forks source link

Meta data: maximum number of colors and palette type #5

Closed mtennekes closed 8 months ago

mtennekes commented 3 years ago

Do you have meta data of the palettes? Specifically:

So I'm looking for something similar as:

RColorBrewer::brewer.pal.info
#>          maxcolors category colorblind
#> BrBG            11      div       TRUE
#> PiYG            11      div       TRUE
#> PRGn            11      div       TRUE
#> PuOr            11      div       TRUE
#> RdBu            11      div       TRUE
#> RdGy            11      div      FALSE
#> RdYlBu          11      div       TRUE
#> RdYlGn          11      div      FALSE
#> Spectral        11      div      FALSE
#> Accent           8     qual      FALSE
#> Dark2            8     qual       TRUE
#> Paired          12     qual       TRUE
#> Pastel1          9     qual      FALSE
#> Pastel2          8     qual      FALSE
#> Set1             9     qual      FALSE
#> Set2             8     qual       TRUE
#> Set3            12     qual      FALSE
#> Blues            9      seq       TRUE
#> BuGn             9      seq       TRUE
#> BuPu             9      seq       TRUE
#> GnBu             9      seq       TRUE
#> Greens           9      seq       TRUE
#> Greys            9      seq       TRUE
#> Oranges          9      seq       TRUE
#> OrRd             9      seq       TRUE
#> PuBu             9      seq       TRUE
#> PuBuGn           9      seq       TRUE
#> PuRd             9      seq       TRUE
#> Purples          9      seq       TRUE
#> RdPu             9      seq       TRUE
#> Reds             9      seq       TRUE
#> YlGn             9      seq       TRUE
#> YlGnBu           9      seq       TRUE
#> YlOrBr           9      seq       TRUE
#> YlOrRd           9      seq       TRUE

Created on 2021-06-10 by the reprex package (v2.0.0)

We can classify 'rainbow' palettes as sequential. The bivariate palettes (great that you have them!) are a category on their own.

(As you may know I'll use pals for tmap: see https://github.com/mtennekes/tmap/issues/566)

bschilder commented 9 months ago

@kwstat this would be a simple but super useful feature! I use pals for quite a few packages and it would be great to have this information.

@mtennekes in the meantime I wrote a small function to try and get some of this information from an internal object within pals:

pals_info <- function(){
    maxcolors <- NULL;
    syspals <- utils::getFromNamespace("syspals", "pals")
    pdt <- lapply(syspals, function(x){
      if(is.list(x)){
        length(x[[length(x)]])
      } else {
        Inf
      }
    }) |> 
      unlist()|>
      data.table::as.data.table(keep.rownames=TRUE) |>
      `colnames<-`(c("palette","maxcolors"))
    pdt[,is_finite:=is.finite(maxcolors)]
    data.table::setorderv(pdt,"maxcolors",na.last=TRUE)
    return(pdt)
  } 
pdt <- pals_info()

Screenshot 2023-12-11 at 21 24 48

This is_finite column is least helpful when determining which palettes wouldn't be suitable when you need some unknown large number of colors.

@kwstat let me know if a version of this would be helpful and I can make a PR.

bschilder commented 9 months ago

Actually, that function I provided wasn't very accurate. This one should work much better as I'm inferring the max number of colors from the default arguments in each function (after checking that it really is an exported function):

pals_info <- function(){
    maxcolors <- NULL;
    ns <- getNamespaceExports("pals")
    syspals <- utils::getFromNamespace("syspals", "pals")
    pdt <- sapply(names(syspals)[names(syspals) %in% ns], 
                  function(p){
      f <- formals(utils::getFromNamespace(p,"pals"))
      if(!"n" %in% names(f)) return(NA)
      if(rlang::is_missing(f$n)) Inf else eval(f$n)
    }) |> 
      data.table::as.data.table(keep.rownames=TRUE) |>
      `colnames<-`(c("palette","maxcolors"))
    pdt[,is_finite:=is.finite(maxcolors)]
    data.table::setorderv(pdt,"maxcolors",na.last=TRUE)
    return(pdt)
  } 
kwstat commented 9 months ago

Is it possible to remove the use of data.table and rlang? Those are currently not included in DEPENDS for the package. I suggest naming the function pals.maxcolors or something similar. This makes it more informative than "info" and using "." makes it consistent with other functions in the package. (I probably should have used "_" instead of "." but that decision was made a LONG time ago).

bschilder commented 9 months ago

Is it possible to remove the use of data.table and rlang? Those are currently not included in DEPENDS for the package. I suggest naming the function pals.maxcolors or something similar. This makes it more informative than "info" and using "." makes it consistent with other functions in the package. (I probably should have used "_" instead of "." but that decision was made a LONG time ago).

Sure! that should be easy enough. I can make a PR soon.

bschilder commented 8 months ago

Just made the PR here @mtennekes @kwstat :

kwstat commented 8 months ago

Closed via #11