Keywords: R programming | data visualization | colorblind accessible design
Abstract: This article explores how to select 4-8 colors in base R to create colorblind-friendly visualizations. By analyzing the Okabe-Ito palette, the R4 default palette, and sequential/diverging palettes provided by the hcl.colors() function, it details the design principles and applications of these tools for color accessibility. Practical code examples demonstrate manual creation and validation of color combinations to ensure readability for individuals with various types of color vision deficiencies.
Background and Importance of Colorblind-Friendly Visualizations
In data visualization, color is a key element for distinguishing categories or representing numerical variations. However, approximately 8% of men and 0.5% of women have some form of color vision deficiency, with red-green colorblindness being the most common. This means traditional color choices (e.g., red and green) may prevent these individuals from accurately interpreting chart information. Therefore, designing colorblind-accessible color combinations is not only a requirement for inclusive design but also a fundamental principle of scientific communication.
Colorblind-Friendly Palettes in Base R
Since R version 4.0.0, base R introduced the palette.colors() function specifically to provide predefined palettes. Among these, the Okabe-Ito palette, set as the default, has been extensively validated as colorblind-friendly. This palette includes eight colors: black, orange, sky blue, bluish green, yellow, blue, vermilion, and reddish purple. These colors maintain high discriminability under color vision deficiencies.
# Retrieve the Okabe-Ito palette
colors <- palette.colors(palette = "Okabe-Ito")
print(colors)
# Sample output:
# black orange skyblue bluishgreen yellow
# "#000000" "#E69F00" "#56B4E9" "#009E73" "#F0E442"
# blue vermillion reddishpurple gray
# "#0072B2" "#D55E00" "#CC79A7" "#999999"
In addition to the Okabe-Ito palette, base R offers other qualitative palettes, such as the "R4" palette. Designed with color accessibility in mind, this palette optimizes hue and luminance contrast to ensure clear differentiation under various color vision conditions. Users can directly call it via palette.colors(palette = "R4").
Colorblind-Friendly Implementation of Sequential and Diverging Palettes
For data requiring sequential or diverging representations, base R's hcl.colors() function (available since R 3.6.0) provides multiple colorblind-friendly options. Based on the HCL (hue-chroma-luminance) color model, this function generates color gradients that remain readable under color vision deficiencies.
# Using the viridis palette (default, colorblind-friendly)
sequential_palette <- hcl.colors(n = 10, palette = "viridis")
# Using a colorblind-friendly diverging palette from ColorBrewer
diverging_palette <- hcl.colors(n = 11, palette = "Blue-Red 2")
# Visualization example
plot(1:10, col = sequential_palette, pch = 19, cex = 2)
The hcl.colors() function supports various predefined palettes, including colorblind-friendly options from ColorBrewer, viridis, and CARTO colors. These palettes are tested through simulations of colorblind vision to ensure robustness in practical applications.
Manual Creation and Validation of Colorblind-Friendly Color Combinations
While predefined palettes offer convenient solutions, users may need to manually create color combinations for specific requirements. The following example, based on the principles of the Okabe-Ito palette, demonstrates how to select 4-8 colors and validate their color accessibility.
# Define manual color combination (core colors from Okabe-Ito palette)
manual_colors <- c("#E69F00", "#56B4E9", "#009E73", "#0072B2",
"#D55E00", "#CC79A7", "#F0E442", "#000000")
# Create a pie chart to visualize color discriminability
pie(rep(1, 8), col = manual_colors,
labels = c("Orange", "Sky Blue", "Bluish Green", "Blue",
"Vermilion", "Reddish Purple", "Yellow", "Black"))
# Simulate colorblind vision using grayscale conversion (approximate method)
gray_colors <- rgb2gray(col2rgb(manual_colors))
pie(rep(1, 8), col = gray_colors, main = "Grayscale Simulation")
When manually creating colors, adhere to these principles: avoid red-green combinations; ensure sufficient differences in luminance and saturation; use online tools (e.g., Color Oracle) to simulate colorblind vision for testing. Base R functions like col2rgb() and rgb2gray() can be used for preliminary grayscale conversion analysis.
Practical Recommendations and Conclusion
In practice, it is advisable to prioritize base R's built-in colorblind-friendly palettes, such as Okabe-Ito or options from hcl.colors(). For scenarios requiring custom colors, refer to colorblind-friendly design guidelines and validate effectiveness through simulation tests. Additionally, combining other visual encodings (e.g., shapes, textures) can further enhance chart accessibility.
Base R has significantly strengthened support for colorblind-friendly visualizations in recent years. Through functions like palette.colors() and hcl.colors(), users can easily create inclusive charts. These tools not only enhance the scientific value of data visualizations but also reflect respect for diversity.