paletti icon indicating copy to clipboard operation
paletti copied to clipboard

get_scale_list fails for continuous list-based palettes

Open bvancil opened this issue 5 years ago • 1 comments

library("ggplot2")
# Installed using:
# remotes::install_github("edwinth/paletti", INSTALL_opts = c("--no-multiarch"))
library("paletti")

# Based off the README.md example but using a list-based palette
my_comp_col  <- get_scale_color(get_pal(list(
  palette_1 = my_company_cols[1L:2L],
  palette_2 = my_company_cols[2L:3L]
)))

# Plotting works for discrete list-based palettes.
ggplot(mtcars, aes(mpg, drat, color = as.character(cyl))) +
  geom_point(size = 4) +
  my_comp_col(palette = "palette_1", discrete = TRUE)


# The plot works with the default continuous palette.
ggplot(mtcars, aes(mpg, drat, color = wt)) +
  geom_point(size = 4)


# The plot fails when trying to use the custom continuous palette.
ggplot(mtcars, aes(mpg, drat, color = wt)) +
  geom_point(size = 4) +
  my_comp_col(palette = "palette_1", discrete = FALSE)
#> Error in pal_object(palette = palette, palette_list = palette_list, alpha = alpha, : could not find function "pal_object"

# Can we patch to make it work?
get_scale_list_original <- paletti::get_scale_list
dput(get_scale_list_original)
#> function (pal_object, scale_type = "colour") 
#> {
#>     palette_func <- pal_object$pal_func
#>     palette_list <- pal_object$palette_list
#>     function(palette = NULL, discrete = TRUE, alpha = 1, reverse = FALSE, 
#>         ...) {
#>         if (is.null(palette)) 
#>             palette <- names(palette_list)[1]
#>         if (discrete) {
#>             discrete_scale(scale_type, "thank_you_ochRe_team", 
#>                 palette = palette_func(palette = palette, palette_list = palette_list, 
#>                   alpha = alpha, reverse = reverse))
#>         }
#>         else {
#>             func <- ifelse(scale_type == "colour", scale_color_gradientn, 
#>                 scale_fill_gradientn)
#>             func(colours = pal_object(palette = palette, palette_list = palette_list, 
#>                 alpha = alpha, reverse = reverse, ...)(256))
#>         }
#>     }
#> }
env <- asNamespace("paletti")
unlockBinding("get_scale_list", env)
# Change "pal_object" to "palette_func" for continuous case.
get_scale_list_modified <- function (pal_object, scale_type = "colour")
{
  palette_func <- pal_object$pal_func
  palette_list <- pal_object$palette_list
  function(palette = NULL,
           discrete = TRUE,
           alpha = 1,
           reverse = FALSE,
           ...) {
    if (is.null(palette))
      palette <- names(palette_list)[1]
    if (discrete) {
      discrete_scale(
        scale_type,
        "thank_you_ochRe_team",
        palette = palette_func(
          palette = palette,
          palette_list = palette_list,
          alpha = alpha,
          reverse = reverse
        )
      )
    }
    else {
      func <- ifelse(scale_type == "colour",
                     scale_color_gradientn,
                     scale_fill_gradientn)
      func(
        colours = palette_func(
          palette = palette,
          palette_list = palette_list,
          alpha = alpha,
          reverse = reverse,
          ...
        )(256)
      )
    }
  }
}
assignInNamespace(
  x = "get_scale_list", 
  value = get_scale_list_modified,
  ns = "paletti", 
  envir = env
)
lockBinding("get_scale_list", env)

# The plot now works for the continuous list-based palette.
my_comp_col  <- get_scale_color(get_pal(list(
  palette_1 = my_company_cols[1L:2L],
  palette_2 = my_company_cols[2L:3L]
)))
ggplot(mtcars, aes(mpg, drat, color = wt)) +
  geom_point(size = 4) +
  my_comp_col(palette = "palette_1", discrete = FALSE)

Created on 2020-06-23 by the reprex package (v0.3.0)

bvancil avatar Jun 23 '20 14:06 bvancil

#2 should fix this.

bvancil avatar Jun 25 '20 14:06 bvancil