Colorblind friendly
Packages: dplyr, ggplot2, viridis, colorBlindness, RColorBrewer
About 1 in 20 people are colorblind in some way. Therefore, a general recommendation is to avoid problematic color combinations or lack of contrast and luminosity difference, and instead, use shapes, sizes, textures, or labels to convey information. Nonetheless, colorblind-friendly palettes have been created to provide options for those who choose to use colors in their data visualization and to make them accessible to colorblind people.
Color basics in ggplot2
In ggplot2, colors can be specified using a variety of functions and arguments. You can attribute a color with a variable by specifying one of the aesthetic values such as ‘color’ or ‘fill’. If the variable provided is numeric, a gradient scale will be used. If the variable is a character or factor one, a categorical scale will be used. We can easily change a numerical variable to a factor one using the ‘factor()’ function within ggplot2 functions.
ggplot(data, aes(x = PA1, y = PA2, color = factor(contact))) +
geom_point()
Later, you can change the colors taken by the aesthetic values. To do so, you should consider the aesthetic (e.g., fill or/and color) you want to change and the type of variables that you have specified (e.g., discrete, continuous):
- The
scale_..._manual()
functions are used to specify a custom color palette:- ‘scale_color_manual()’
- ‘scale_fill_manual()’
- The
scale_..._continuous()
functions are used for continuous variables and include:- ‘scale_color_continuous()’
- ‘scale_fill_continuous()’
- The
scale_..._discrete()
functions are used for discrete variables and include:- ‘scale_color_discrete()’
- ‘scale_fill_discrete()’
- There are also specialized functions for specific types of data, such as:
- ‘scale_color_brewer()’: For using color palettes from the ColorBrewer website.
- ‘scale_color_gradient()’: For using a gradient of colors between two endpoints.
You can create your own color palette or take a pre-defined one from a package like RColorBrewer or viridis. Then, to apply a color palette, you can specify the palette using the values argument in the scale_..._discrete()
or scale_..._continuous()
functions. The palette argument takes a vector of colors that will be used for the plot.
# Creation of the palette
= c("#8ECAE6", "#219EBC", "#023047","#FFB703","#FB8500")
palette
# Application of the palette
ggplot(data, aes(x = PA1, y = PA2, color = factor(location))) +
geom_point() +
scale_color_manual(values = palette)
You can find pre-made palettes of color that are colorblind-friendly, for example here. In addition, tools exist to help you build your palette, for instance here.
viridis R package
The viridis R package provides a set of color palettes that are designed to be colorblind friendly, colorful, and perceptually uniform (values close to each other have very similar-appearing colors, and vice-versa). It contains eight color scales.
library(viridis)
It is a very good package in practice as it is easy to apply to any plot with the functions ‘scale_color_viridis()’ and ‘scale_fill_viridis()’. In both functions:
- ‘discrete’ argument informs if the scale used is continuous or discrete (discrete=TRUE).
- ‘option’ argument defines palette of viridis to be used, using the name of the palette or a letter associated: ‘magma’ (‘A’), ‘inferno’ (‘B’), ‘plasma’ (‘C’), ‘viridis’ (‘D’), ‘cividis’ (‘E’), ‘rocket’ (‘F’), ‘mako’ (‘G’), ‘turbo’ (‘H’).
ggplot(data, aes(x = PA1, y = PA2, color=factor(contact))) +
geom_point() +
scale_color_viridis(discrete=TRUE, option="B")
For more information see the vignette of the package.
colorBlindness R package
The colorBlindness package provides functions to simulate different types of color blindness and tools to modify colors to enhance their visibility for those with color vision deficiencies. The package can be used to test plots for accessibility and to adjust color palettes for improved visibility.
library(colorBlindness)
To test one of your plots made with ggplot2 you can use the function ‘cvdPlot()’.
# Create my plot
= data %>%
p ggplot(aes(x=PA1, y=PA2, color=factor(location))) +
geom_point()
# Check my plot with the cvdPlot function
cvdPlot(p)
The output displays four plots:
- The plot seen with normal vision.
- The plot seen if you had deuteranopia.
- The plot seen if you had protanopia.
- The plot in black and white (might be important to consider when printing the plot in a black and white version).
A good combination of colors would allow you to distinguish the difference in at least the 3 first plots and, ideally, the fourth too. Below, we test a palette of the viridis package and we can see that it is indeed distinguishable in every plot.
# Create my plot
= data %>%
p ggplot(aes(x=PA1, y=PA2, color=factor(location))) +
geom_point() +
scale_color_viridis(discrete=TRUE) +
theme_bw()
# Check my plot with the cvdPlot function
cvdPlot(p)
This package also contains safe color palette that can be selected using ‘displayAvailablePalette()’:
displayAvailablePalette()
Those palettes can be used with the ‘scale_color_manual()’ function. Below, we use the Blue2DarkOrange18Steps color palette:
%>%
data ggplot(aes(x=PA1, y=PA2, color=factor(location))) +
geom_point() +
scale_color_manual(values=Blue2DarkOrange18Steps)
For more information see the vignette of the package.
RColorBrewer packages
The RColorBrewer is a very popular package that contains palettes of color. They are designed to be easy to interpret by people with color vision deficiencies and are useful for creating aesthetically pleasing plots.
library(RColorBrewer)
After importing the package, the palette of color names can be found in the ‘brewer.pal.info’ dataframe. They are categorized as followed:
- seq for sequential, which consists of colors varying in brightness
- div for diverging, which contains two contrasting hues. Particularly useful to contrast positive and negative values.
- qual for qualitative, which contains multiple distinct hues, useful to represent categorical data.
To select a palette, you can use the colorbrewer2 website which allows you to select, configure and visualize all the palettes of the RColorBrewer package. You can also display the palette names and filter for the colorblind-friendly ones, as follows:
$colorblind==TRUE,] brewer.pal.info[brewer.pal.info
maxcolors category colorblind
BrBG 11 div TRUE
PiYG 11 div TRUE
PRGn 11 div TRUE
PuOr 11 div TRUE
RdBu 11 div TRUE
RdYlBu 11 div TRUE
Dark2 8 qual TRUE
Paired 12 qual TRUE
Set2 8 qual TRUE
Blues 9 seq TRUE
BuGn 9 seq TRUE
BuPu 9 seq TRUE
GnBu 9 seq TRUE
Greens 9 seq TRUE
Greys 9 seq TRUE
Oranges 9 seq TRUE
OrRd 9 seq TRUE
PuBu 9 seq TRUE
PuBuGn 9 seq TRUE
PuRd 9 seq TRUE
Purples 9 seq TRUE
RdPu 9 seq TRUE
Reds 9 seq TRUE
YlGn 9 seq TRUE
YlGnBu 9 seq TRUE
YlOrBr 9 seq TRUE
YlOrRd 9 seq TRUE
To use the palettes, you need to use the functions ‘scale_fill_brewer()’ or ‘scale_colour_brewer()’. Below, we use the ‘Dark2’ color palette:
%>%
data ggplot(aes(x=PA1, y=PA2, fill=factor(location))) +
geom_bar(stat="identity") +
scale_fill_brewer(palette="Dark2")
For more information see: https://www.datanovia.com/en/blog/the-a-z-of-rcolorbrewer-palette/