Created
July 6, 2020 10:37
-
-
Save thibautjombart/a634555e218ce5a9695e4b22fb57152e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Source: color palettes come from https://personal.sron.nl/~pault/#sec:qualitative | |
## They exclude 'grey', which will be reserved for missing data. | |
col_vibrant <- c( | |
"#0077BB", | |
"#33BBEE", | |
"#009988", | |
"#EE7733", | |
"#CC3311", | |
"#EE3377" | |
) | |
col_muted <- c( | |
"#332288", | |
"#88CCEE", | |
"#44AA99", | |
"#117733", | |
"#999933", | |
"#DDCC77", | |
"#CC6677", | |
"#882255", | |
"#AA4499" | |
) | |
make_palette <- function(x, quiet = FALSE, suggest = NULL) { | |
function(n) { | |
if (n <= length(x)) { | |
x[seq_len(n)] | |
} else { | |
if (!quiet) { | |
msg <- sprintf( | |
paste("Using more colors (%d) than this palette can handle (%d);", | |
"some colors will be interpolated."), | |
n, | |
length(x) | |
) | |
if (!is.null(suggest)) { | |
msg <- paste0( | |
msg, | |
sprintf("\nConsider using `%s` palette instead?", | |
suggest) | |
) | |
} | |
warning(msg) | |
} | |
colorRampPalette(x)(n) | |
} | |
} | |
} | |
## Example | |
### works nicely | |
make_palette(col_vibrant)(4) | |
### issues a warning | |
make_palette(col_vibrant)(8) | |
### issues a warning and suggestion | |
make_palette(col_vibrant, suggest = "foobar")(8) | |
## works nicely | |
make_palette(col_muted, suggest = "foobar")(8) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created on 2020-07-06 by the reprex package (v0.3.0)