Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default color palette #11

Closed
1 of 3 tasks
thibautjombart opened this issue Jul 3, 2020 · 10 comments
Closed
1 of 3 tasks

Default color palette #11

thibautjombart opened this issue Jul 3, 2020 · 10 comments
Labels
discussion FR / enhancement New feature or request help wanted Extra attention is needed

Comments

@thibautjombart
Copy link
Contributor

thibautjombart commented Jul 3, 2020

The 3 requirements of the new color palette would be:

  • look nice to as many humans as possible
  • be colorblind friendly
  • correspond to categorical variables

Quite a bit of thinking on these has been done by the viridis package:
https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html

As well, these very good resources:

I suggest we use this issue to propose palettes. Ideally put them to a vote at some point.

@thibautjombart thibautjombart added discussion FR / enhancement New feature or request help wanted Extra attention is needed labels Jul 3, 2020
@TimTaylor
Copy link
Collaborator

I'm colourblind (red/green) so hopefully help out with the testing of palettes.

With the release of R 4.0 (see https://developer.r-project.org/Blog/public/2019/11/21/a-new-palette-for-r/index.html). Sometimes I find the most colourblind-friendly palettes are not necessarily the nicest looking (this is subjective of course). One additional thing to think about is palette ordering. Sometimes you can get away with a less friendly palette if you are only using a small subset of them.

@thibautjombart
Copy link
Contributor Author

Not that I'm nessarily advocating for it, but here's one, derived from 'paired' in RColorBrewer, which natively allows up to 12 levels; not color-blind friendly I fear:

## trying colors
### paired tweaked
incidence_pal <- function(n) {
  col <- c("#1F78B4", "#33A02C", "#E31A1C", "#FF7F00", "#6A3D9A", "#B15928", 
           "#A6CEE3", "#B2DF8A", "#FB9A99", "#FDBF6F", "#CAB2D6", "#FFFF99"
           )
  if (n < length(col)) {
    return(col[seq_len(n)])
  }

  colorRampPalette(col)(n)
}


barplot(1:12, col = incidence_pal(12), main = "palette: sorted `paired`")

## 3 colors
barplot(matrix(sample(1:10, 1000, replace = TRUE), nrow = 3, ncol = 60),
        col = incidence_pal(3), border = "transparent")
#> Warning in matrix(sample(1:10, 1000, replace = TRUE), nrow = 3, ncol = 60): data
#> length [1000] is not a sub-multiple or multiple of the number of rows [3]

## 8 colors
barplot(matrix(sample(1:10, 1000, replace = TRUE), nrow = 8, ncol = 60),
        col = incidence_pal(8), border = "transparent")
#> Warning in matrix(sample(1:10, 1000, replace = TRUE), nrow = 8, ncol = 60): data
#> length [1000] is not a sub-multiple or multiple of the number of columns [60]

## 12 colors
barplot(matrix(sample(1:10, 1000, replace = TRUE), nrow = 12, ncol = 60),
        col = incidence_pal(12), border = "transparent")
#> Warning in matrix(sample(1:10, 1000, replace = TRUE), nrow = 12, ncol = 60):
#> data length [1000] is not a sub-multiple or multiple of the number of rows [12]

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

@TimTaylor
Copy link
Collaborator

From left to right, drop colours 1, 2, 3 and 7 and it works for me

incidence_pal <- function(n) {
  col <- c("#FF7F00", "#6A3D9A", "#B15928", "#B2DF8A", "#FB9A99", "#FDBF6F", "#CAB2D6", "#FFFF99"
  )
  if (n < length(col)) {
    return(col[seq_len(n)])
  }
  
  colorRampPalette(col)(n)
}


barplot(1:8, col = incidence_pal(8), main = "palette: reduced sorted `paired`")

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

@thibautjombart
Copy link
Contributor Author

This one is derived from the colorbling-friendly categorical variable palettes at: https://personal.sron.nl/~pault/#sec:qualitative
Two versions:

  • vibrant: my favourite, less categories
  • muted: nice too, more categories

I think I also like the colors better, but how does it work for you?

## my preferred, uses https://personal.sron.nl/~pault/#sec:qualitative
col_vibrant <- c(
  "#0077BB",
  "#33BBEE",
  "#009988",
  "#EE7733",
  "#CC3311",
  "#EE3377",
  "#BBBBBB"
  )

col_muted <- c(
  "#332288",
  "#88CCEE",
  "#44AA99",
  "#117733",
  "#999933",
  "#DDCC77",
  "#CC6677",
  "#882255",
  "#AA4499",
  "#BBBBBB"
  )


make_palette <- function(x) {
  function(n) {
    if (length(x) > n) {
      x[seq_len(n)]
    } else {
      colorRampPalette(x)(n)
    }
  }
}


#### vibrant palette
pal_vibrant <- make_palette(col_vibrant)
barplot(1:7, col = pal_vibrant(7), main = "palette: vibrant")

barplot(matrix(sample(1:10, 1000, replace = TRUE), nrow = 3, ncol = 60),
        col = pal_vibrant(3))
#> Warning in matrix(sample(1:10, 1000, replace = TRUE), nrow = 3, ncol = 60): data
#> length [1000] is not a sub-multiple or multiple of the number of rows [3]

barplot(matrix(sample(1:10, 1000, replace = TRUE), nrow = 8, ncol = 60),
        col = pal_vibrant(8))
#> Warning in matrix(sample(1:10, 1000, replace = TRUE), nrow = 8, ncol = 60): data
#> length [1000] is not a sub-multiple or multiple of the number of columns [60]

#### muted palette
pal_muted <- make_palette(col_muted)
barplot(1:7, col = pal_muted(7), main = "palette: muted")

barplot(matrix(sample(1:10, 1000, replace = TRUE), nrow = 3, ncol = 60),
        col = pal_muted(3))
#> Warning in matrix(sample(1:10, 1000, replace = TRUE), nrow = 3, ncol = 60): data
#> length [1000] is not a sub-multiple or multiple of the number of rows [3]

barplot(matrix(sample(1:10, 1000, replace = TRUE), nrow = 10, ncol = 60),
        col = pal_muted(10))
#> Warning in matrix(sample(1:10, 1000, replace = TRUE), nrow = 10, ncol = 60):
#> data length [1000] is not a sub-multiple or multiple of the number of columns
#> [60]

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

@TimTaylor
Copy link
Collaborator

Vibrant is best for me. Which colour would you use as the default for facets and ungrouped plots?

@thibautjombart
Copy link
Contributor Author

Awesome. Maybe the first of the palette as default color for single plots / no groups? This said, feel free to reorder the colors of the palette if you fancy.

@TimTaylor
Copy link
Collaborator

Now implemented. What do you think about the default for the column borders (white or NA)

@thibautjombart
Copy link
Contributor Author

Morning! I was trying to test both to compare and found this issue. I think it may come from the borders (bars are so thin we see only the border)? If that is the case, that could be an element to consider in the default. My thinking would be:

  • transparent border: safer for thin bars (if that's the issue)
  • grey / black border: may be better contrast between colors (not even sure if that's true)

@thibautjombart
Copy link
Contributor Author

On an unerlated point: maybe it would make sense to plot the missing (NA) group as grey?

@TimTaylor
Copy link
Collaborator

Are we happy to close this now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion FR / enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants