Skip to content

Instantly share code, notes, and snippets.

@thibautjombart
Created July 6, 2020 10:37
Show Gist options
  • Save thibautjombart/a634555e218ce5a9695e4b22fb57152e to your computer and use it in GitHub Desktop.
Save thibautjombart/a634555e218ce5a9695e4b22fb57152e to your computer and use it in GitHub Desktop.
## 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)
@thibautjombart
Copy link
Author

## 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)
#> [1] "#0077BB" "#33BBEE" "#009988" "#EE7733"

### issues a warning
make_palette(col_vibrant)(8)
#> Warning in make_palette(col_vibrant)(8): Using more colors (8) than this palette
#> can handle (6); some colors will be interpolated.
#> [1] "#0077BB" "#24A7DF" "#1DACC2" "#21947B" "#CB7B3F" "#DA501F" "#D5332E"
#> [8] "#EE3377"

### issues a warning and suggestion
make_palette(col_vibrant, suggest = "foobar")(8)
#> Warning in make_palette(col_vibrant, suggest = "foobar")(8): Using more colors (8) than this palette can handle (6); some colors will be interpolated.
#> Consider using `foobar` palette instead?
#> [1] "#0077BB" "#24A7DF" "#1DACC2" "#21947B" "#CB7B3F" "#DA501F" "#D5332E"
#> [8] "#EE3377"

## works nicely
make_palette(col_muted, suggest = "foobar")(8)
#> [1] "#332288" "#88CCEE" "#44AA99" "#117733" "#999933" "#DDCC77" "#CC6677"
#> [8] "#882255"

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment