Repel Visually Similar Colors for Colorblind Users in Various Plots

Iterate and repel visually similar colors away in various 'ggplot2' plots. When many groups are plotted at the same time on multiple axes, for instance stacked bars or scatter plots, effectively ordering colors becomes difficult. This tool iterates through color combinations to find the best solution to maximize visual distinctness of nearby groups, so plots are more friendly toward colorblind users. This is achieved by two distance measurements, distance between groups within the plot, and CIELAB color space distances between colors as described in Carter et al., (2018) .


color_repel

CRAN status

Repel visually similar colors away for colorblind users

For work (single cell RNA-seq) I make and look at countless plots. Though most packages attempt to be colorblind-aware/friendly, I often find results uninterpretable when over a handful of colors are used. Some helpful strategies include: scatterHatch, adding different hatch patterns to clusters on top of colors; ggtrace; plotly interactivity; etc. But perhaps a simpler solution can be used -- avoid using visually similar colors next to each other (ie. on a UMAP, neighboring clusters should never be light yellow and slightly darker yellow). In 1D plots (barplot, violin plots, etc), we'd simply pass a vector of dissimilar colors. Not so easy to do when 2D and many colors are involved. Hence this simple package.

How this works

  1. extract colors from plot object or just use a desired vector of colors
  2. generate distance matrix of categories (clusters on 2D plot, group on other types of plots)
  3. generate distance matrix of colors, after conversion to CIELab space (and possibly various colorblindness conversion functions)
  4. find optimal assignments of color to above groups/clusters to avoid plotting visually similar colors near each other
  5. recolor (with option to add repelled text label for each group as well)
  6. add encircle geom if desired

Installation

# now on CRAN
install.packages("colorrepel")

# or latest devel version from github
devtools::install_github("https://github.com/raysinensis/color_repel")

library(colorrepel)
?gg_color_repel # overall wrapper function
?scale_color_repel # now compatible with ggplot grammar

Examples

a <- Seurat::DimPlot(so, group.by = "type", pt.size = 0.5)
b <- a + scale_color_manual(values = color_repel(a, sim = colorspace::tritan))
cowplot::plot_grid(a, b, 
                   labels = c("original", "color_repel"))
# now also works with a + scale_color_repel(); a + scale_fill_repel()
a <- readRDS("bar_gg.rds")
# `color_repel` reorders colors
b <- a + scale_fill_manual(values = color_repel(a, col = "fill"))
# or use wrapper, with option to recolor with `polychrome`- better suited for 20+ colors
c <- gg_color_repel(a, sim = colorspace::deutan, polychrome_recolor = TRUE)
cowplot::plot_grid(a, b, c, labels = c("default", "color_repel", "polychrome"), nrow = 1)
a <- Seurat::DimPlot(so, group.by = "type", pt.size = 0.5)
b <- gg_color_repel(a, repel_label = T)
c <- gg_color_repel(a, encircle = T)
d <- gg_color_repel(a, mascarade = T)
cowplot::plot_grid(b, c, d, 
  labels = c("label_repel", "encircle", "mascarade"),
  nrow = 1
)

Also see it in action here: https://raysinensis.shinyapps.io/spatialshiny_adr/

-->

For layering non-interactive annotations and interactive points, see example: https://raysinensis.github.io/color_repel/

Citation

In situ reconstruction of distinct normal and pathological cell populations within the human adrenal gland. Journal of the Endocrine Society, 2023. Fu R, Walters K, Kaufman M, Kocs K, Baldwin A, Kiseljak-Vassiliades K, Fishbein L, Mukherjee N. https://doi.org/10.1210/jendso/bvad131

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("colorrepel")

0.5.2 by Rui Fu, 2 months ago


https://github.com/raysinensis/color_repel


Browse source code at https://github.com/cran/colorrepel


Authors: Rui Fu [cre, aut, cph]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports grDevices, matrixStats, Matrix, distances, stats, dqrng, gtools, purrr, dplyr, plyr, stringr, ggplot2, ggrepel, plotly, knitr, png, Polychrome, mascarade

Suggests colorspace


Suggested by mascarade.


See at CRAN