clauswilke / colorblindr

An R package to simulate colorblindness on R figures.
MIT License
306 stars 29 forks source link

Just checking - no continuous functionality? #16

Open jzadra opened 4 years ago

jzadra commented 4 years ago

I juist wanted to confirm that this package doesn't support continuous scales? I'm not missing anything?

dnkent commented 3 years ago

@jzadra, ran across this question while looking for an option to produce more than 8 colors. In my case, I ended up putting together this hacky fix for creating a lengthier palette based on the colorblindr defaults. Once the palette is generated, then it can be inserted into ggplot with scale_fill_manual(values = colorblindr_cont).

When my monitor is set to grayscale it seems to generally maintain the desired colorblindr differentiation. Maybe this will work for your needs or be easily adjusted if not?

library(RColorBrewer)

gen_palette <- function(data, var){
  n_colors <- length(unique(data[, var])) # number of unique values for var
  color_pal <- c(
    "#E69F00", "#56B4E9", "#009E73", "#F0E442",
    "#0072B2", "#D55E00", "#CC79A7", "#000000"
  )
  custom_colors <- colorRampPalette(color_pal)(n_colors)
  return(custom_colors)
}

colorblinr_cont <- gen_palette(data = dataset, var = "varname")
jzadra commented 3 years ago

Thanks!