# colorBlindness Guide

#### Jianhong Ou

#### 2020-03-09

# Introduction

Color blindness is color vision deficiency, which means people with color blindness may be not aware of differences among colors that are obvious to the people without it. This package is designed to provide a collection of safe colors for plots such as heatmaps, pie chart, and try to find a solution for acceptable figures for all the end users.

## Safe colorsets

There are lots of packages already collected lots of safe colors. I list some of them, but not limited here.

- munsell

- viridis and RColorBrewer

- dichromat

- colorblindr

- shades

- ggsci

For this package, I will try to collect as much as possible safe color sets ready to use.

## Color vision deficiency (CVD) simulator

The tool used to simulate the color vision deficiency is called CVD simulator. I list some of packages here with CVD simulator.

- Hans Brettel 1997(Brettel et al., 1997)

- Francoise Vienot 1999(Viénot et al., 1999) (implemented in this package and package dichromat )

- Gustavo M. Machado 2009(Machado et al., 2009) (implemented in package colorspace)

- Claire D. McWhite and Claus O. Wilke. colorblindr

# How to use this package

## Use safe color to plot

To plot figures by colors of this package, there are three steps:

1. select correct safe color set.

2. plot with the color set.

3. check the plot by CVD simulator.

For example if we want to plot a heatmap.

    library(colorBlindness)
    mat <- matrix(1:81, nrow = 9, ncol = 9)

    library(ggplot2)
    library(reshape2)
    mat1 <- melt(t(mat[9:1, ]))
    len <- length(Blue2DarkRed12Steps)-1
    mat1$v2 <- cut(mat1$value,
                  breaks = seq(0,ceiling(81/len)*len,
                               length.out = len+1))
    ht <- ggplot(mat1) +
      geom_tile(aes(x=Var1, y=Var2, fill=v2)) +
      scale_fill_manual(values=Blue2DarkRed12Steps) +
      theme_bw()
    # check the plot by CVD simulator
    cvdPlot(ht)

## replace the color of a plot to safe color

If you have figures which is not safe to color blindness people, you can try to replace the colors by `replacePlotColor` function.

    cvdPlot(replacePlotColor(displayColors(c("Red", "Green", "blue"))))

If you have a fluorescence image and want to change the colors, you can read the figure and view the effect of color changed figure. If it is acceptable, you can draw it into a file.

    library(grid)
    library(png)
    path <- system.file("extdata", package = "colorBlindness")
    img <- readPNG(file.path(path, "fluorescence.microscopy.images.png"))
    g <- rasterGrob(img, interpolate=TRUE)
    grid.draw(cvdPlot(g,
                      layout = c("origin", "deuteranope", "protanope",
                                 "enhanced", "enhanced.deuteranope",
                                 "enhanced.protanope")))

    newImg <- replacePlotColor(g)
    outfile <- tempfile(fileext = ".png")
    png(outfile)
    grid.draw(newImg)
    dev.off()

    ## quartz_off_screen
    ##                 2

## Best approach to make figures

To plot figures for literature, there serveral things we need to consider:

1. Figure format and size;

2. Font family and size;

3. Proper figure colors that are distinguishable by people with colorblindness and for printed in black and white.

`setPDFopt` function can provide you preset figure size for PDF outputs.

    pdf.options()[c("width", "height", "pointsize")]

    ## $width
    ## [1] 7
    ##
    ## $height
    ## [1] 7
    ##
    ## $pointsize
    ## [1] 12

    setPDFopt()
    pdf.options()[c("width", "height", "pointsize")]

    ## $width
    ## 1col
    ## 3.54
    ##
    ## $height
    ## 1col
    ## 3.54
    ##
    ## $pointsize
    ## [1] 8

# Collection of safe colors

There are more than 15 Color palettes for color blindness from Martin Krzywinski and et al.

See also GETTING INTO VISUALIZATION OF LARGE BIOLOGICAL DATA SETS and Color Schemes Appropriate for Scientific Data Graphics.

    displayAvailablePalette(color="white")

The `displayAllColors` funciton can be used to show the effect for color vision deficiency. Here we dispaly color collection from Bang Wong(Wong, 2011).

    displayAllColors(safeColors, color="white")

# `replacePlotColor` effect for Ishihara Color Test.

    ## replace the color of a file

    ishihara29 <- system.file("extdata", "Plate3.png", package = "colorBlindness")
    library(png)
    img <- readPNG(ishihara29)
    g <- rasterGrob(img, interpolate=TRUE)
    grid.draw(cvdPlot(g,
                      layout = c("origin", "deuteranope", "protanope",
                                 "enhanced", "enhanced.deuteranope",
                                 "enhanced.protanope")))

# Session Info

    sessionInfo()

    ## R version 3.6.2 (2019-12-12)
    ## Platform: x86_64-apple-darwin15.6.0 (64-bit)
    ## Running under: macOS Catalina 10.15.1
    ##
    ## Matrix products: default
    ## BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
    ## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
    ##
    ## locale:
    ## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    ##
    ## attached base packages:
    ## [1] grid      stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ## [1] png_0.1-7            reshape2_1.4.3       ggplot2_3.3.0
    ## [4] colorBlindness_0.1.6
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] Rcpp_1.0.3         knitr_1.28         magrittr_1.5       tidyselect_1.0.0
    ##  [5] munsell_0.5.0      cowplot_1.0.0      colorspace_1.4-1   R6_2.4.1
    ##  [9] rlang_0.4.5        plyr_1.8.5         stringr_1.4.0      dplyr_0.8.4
    ## [13] tools_3.6.2        gtable_0.3.0       xfun_0.12          withr_2.1.2
    ## [17] gridGraphics_0.5-0 htmltools_0.4.0    assertthat_0.2.1   yaml_2.2.1
    ## [21] digest_0.6.25      tibble_2.1.3       lifecycle_0.2.0    crayon_1.3.4
    ## [25] farver_2.0.3       purrr_0.3.3        glue_1.3.1         evaluate_0.14
    ## [29] rmarkdown_2.1      labeling_0.3       stringi_1.4.6      compiler_3.6.2
    ## [33] pillar_1.4.3       scales_1.1.0       pkgconfig_2.0.3

# References

Brettel, H., Viénot, F., & Mollon, J. D. (1997). Computerized simulation of color appearance for dichromats. JOSA A, 14(10), 2647–2655.

Machado, G. M., Oliveira, M. M., & Fernandes, L. A. (2009). A physiologically-based model for simulation of color vision deficiency. IEEE Transactions on Visualization and Computer Graphics, 15(6), 1291–1298.

Viénot, F., Brettel, H., & Mollon, J. D. (1999). Digital video colourmaps for checking the legibility of displays by dichromats. Color Research & Application: Endorsed by Inter-Society Color Council, the Colour Group (Great Britain), Canadian Society for Color, Color Science Association of Japan, Dutch Society for the Study of Color, the Swedish Colour Centre Foundation, Colour Society of Australia, Centre Français de La Couleur, 24(4), 243–252.

Wong, B. (2011). Points of view: Color blindness. Nature Publishing Group.