Angus Campbell
Angus Campbell

Reputation: 592

How to make ggplot colors more distinct and colorblind friendly?

toLonger <- function(expressionMatrix) {
    expressionMatrix <- longExpressionMatrix <- expressionMatrix %>% 
    as.data.frame() %>%
    rownames_to_column("gene") %>%
    pivot_longer(cols = !gene, 
                 values_to = "Expression",
                 names_to = "sample_id") 
  return(expressionMatrix)
}

toLonger(dge_cpmlogtwo)  %>% 
  ggplot(aes(x = Expression, color = sample_id)) +
  geom_density() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

I want to make the colors in the third last line stand out more? I found this reply but was not able to understand how to apply it to my code.

Lastly is there a way to ensure my plots will be color blind frinedly?

Upvotes: 3

Views: 17863

Answers (1)

Sirius
Sirius

Reputation: 5429

Regarding colorblind friendly palettes, you could consult these two links which address the issue:

  1. https://ggplot2.tidyverse.org/reference/scale_brewer.html
  2. https://colorbrewer2.org/
  3. http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/

I don't have access to your data, but you should be able to click around to find a palette you like, and apply it to your ggplot with the info linked above, good luck!

Here's an example of how it might apply to your case:


toLonger(dge_cpmlogtwo)  %>%
    ggplot(aes(x = Expression, color = sample_id)) +
    geom_density() +
    scale_color_brewer(palette="BrBG") +
    theme(axis.text.x = element_text(angle = 90, hjust = 1))


Also it is aparently not very clear how to find the palette codes in question, eg BrBG, but one way would be like this:

  1. Choose a palette you like from colorbrewer2.org
  2. check the URL for which code it has
  3. Use this code with the palette= argument for eg. scale_color_brewer , like I have done above.

See this image to see what I mean, on it I have the cookbook-r.com and colorbrewer2.org pages from above opened in two windows. Red rectangular highlight is added by me:

detail from relevant web pages

Upvotes: 5

Related Questions