grantmcdermott / tinyplot

Lightweight extension of the base R graphics system
https://grantmcdermott.com/tinyplot
Apache License 2.0
208 stars 7 forks source link

Partial palette name matching could be smarter #73

Closed grantmcdermott closed 10 months ago

grantmcdermott commented 10 months ago

We currently allow for palette matching regardless of case (e.g., "r4" <-> "R4").

But, unlike, the main palette() and hcl.colors() functions, we don't do a good job of partial matching (e.g., with an unambiguous first word like palette("tableau") or hcl.colors(5, "zissou")). It's probably a good idea just to use the same charmatch approach that the latter two functions have adopted internally.

zeileis commented 10 months ago

Yes, I agree. This should work in https://github.com/grantmcdermott/plot2/blob/main/R/by_aesthetics.R#L42-L46

      fx <- function(x) tolower(gsub("[-, _, \\,, (, ), \\ , \\.]", "", x))
      if (!is.na(charmatch(fx(palette), fx(palette.pals())))) {
        palette_fun = palette.colors
      } else if (!is.na(charmatch(fx(palette), fx(hcl.pals())))) {
        palette_fun = hcl.colors
      } else {
      ...

I'm happy to turn this into a proper PR but it may take a couple of days. Hence I wanted to put it here before I forget.