Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tesselle/khroma

:art: Colour Schemes for Scientific Data Visualization
https://github.com/tesselle/khroma

accessibility colour-schemes data-visualization r-package

Last synced: 20 days ago
JSON representation

:art: Colour Schemes for Scientific Data Visualization

Awesome Lists containing this project

README

        

---
output: github_document
bibliography: vignettes/bibliography.bib
---

```{r, include=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = NULL
)
Sys.setlocale("LC_MESSAGES", 'en_GB.UTF-8') # Force locale
```

# khroma

[![R-CMD-check](https://github.com/tesselle/khroma/workflows/R-CMD-check/badge.svg)](https://github.com/tesselle/khroma/actions)
[![codecov](https://codecov.io/gh/tesselle/khroma/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tesselle/khroma)
[![CodeFactor](https://www.codefactor.io/repository/github/tesselle/khroma/badge/main)](https://www.codefactor.io/repository/github/tesselle/khroma/overview/main)
[![Dependencies](https://tinyverse.netlify.app/badge/khroma)](https://cran.r-project.org/package=khroma)

[![r-universe](https://tesselle.r-universe.dev/badges/khroma)](https://tesselle.r-universe.dev/khroma){.pkgdown-devel}
[![CRAN Version](https://www.r-pkg.org/badges/version/khroma)](https://cran.r-project.org/package=khroma){.pkgdown-release}
[![CRAN checks](https://badges.cranchecks.info/worst/khroma.svg)](https://cran.r-project.org/web/checks/check_results_khroma.html){.pkgdown-release}
[![CRAN Downloads](https://cranlogs.r-pkg.org/badges/khroma)](https://cran.r-project.org/package=khroma){.pkgdown-release}

[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)

[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1472077.svg)](https://doi.org/10.5281/zenodo.1472077)

## Overview

Color blindness affects a large number of individuals. When communicating scientific results color palettes must therefore be carefully chosen to be accessible to all readers.

This R package provides an implementation of @okabe2008, @tol2021 and @crameri2018 color schemes. These schemes are ready for each type of data (qualitative, diverging or sequential), with colors that are distinct for all people, including color-blind readers. This package also provides tools to simulate color-blindness and to test how well the colors of any palette are identifiable. To simulate color-blindness in production-ready R figures you may also be interested in the [**colorblindr**](https://github.com/clauswilke/colorblindr) package.

@tol2021 and @crameri2018 offer carefully chosen schemes, ready for each type of data, with colors that are:

* Distinct for all people, including color-blind readers,
* Distinct from black and white,
* Distinct on screen and paper,
* Matching well together,
* Citable and reproducible.

See `vignette("tol")` and `vignette("crameri")` for a more complete overview.

For specific uses, several scientific thematic schemes (geologic timescale, land cover, FAO soils, etc.) are implemented, but these color schemes may not be color-blind safe.

All these color schemes are implemented for use with base R **graphics** or [**ggplot2**](https://github.com/tidyverse/ggplot2) and [**ggraph**](https://github.com/thomasp85/ggraph).

---

```{r citation, echo=FALSE, comment='', results='asis'}
cite <- utils::citation("khroma")
print(cite, bibtex = FALSE)
```

## Installation

You can install the released version of **khroma** from [CRAN](https://CRAN.R-project.org):

```{r cran-installation, eval=FALSE}
install.packages("khroma")
```

And the development version from [GitHub](https://github.com/) with:

```{r gh-installation, eval=FALSE}
# install.packages("remotes")
remotes::install_github("tesselle/khroma")
```

## Usage

```{r packages}
## Install extra packages (if needed)
# install.packages("ggplot2"))

## Load packages
library(khroma)
```

Available palettes (click to expand)

```{r info}
## Get a table of available palettes
info()
```

### Color palettes and scales

`color()` returns a function that when called with a single integer argument returns a vector of colors.

```{r usage}
## Paul Tol's bright color scheme
bright <- color("bright")
bright(7)
```

```{r usage-show, fig.height=2, fig.width=7, fig.align='center'}
## Plot the color scheme
plot_scheme(bright(7), colours = TRUE)
```

```{r usage-plot, fig.height=3.5, fig.width=5, fig.align='center'}
data(mpg, package = "ggplot2")

## Use with graphics
par(mar = c(5, 4, 1, 1) + 0.1)
plot(
x = mpg$displ,
y = mpg$hwy,
pch = 16,
col = palette_color_picker("bright")(mpg$class),
xlab = "displ",
ylab = "hwy",
panel.first = grid(),
las = 1
)

## Use with ggplot2
ggplot2::ggplot(data = mpg) +
ggplot2::aes(x = displ, y = hwy, color = class) +
ggplot2::geom_point() +
ggplot2::theme_bw() +
scale_color_bright()
```

### Diagnostic tools
#### Test how well the colors are identifiable

```{r usage-map, fig.height=2, fig.width=7, fig.align='center'}
## Okabe & Ito's color scheme
okabe <- color("okabe ito")

set.seed(12345)
plot_map(okabe(8))
```

```{r usage-tiles, fig.height=3, fig.width=3, fig.align='center'}
## BuRd sequential color scheme
BuRd <- color("BuRd")

plot_tiles(BuRd(128), n = 256)
```

#### Simulate color-blindness

```{r usage-colorblind1, echo=FALSE, eval=FALSE, fig.height=2, fig.width=7, fig.align='center'}
## change() returns function
deuteranopia <- change(okabe, mode = "deuteranopia")
plot_scheme(deuteranopia(8), colours = TRUE)

protanopia <- change(okabe, mode = "protanopia")
plot_scheme(protanopia(8), colours = TRUE)

tritanopia <- change(okabe, mode = "tritanopia")
plot_scheme(tritanopia(8), colours = TRUE)

achromatopsia <- change(okabe, mode = "achromatopsia")
plot_scheme(achromatopsia(8), colours = TRUE)
```

```{r usage-colorblind2, fig.height=4, fig.width=7, fig.align='center'}
plot_scheme_colorblind(okabe(8))

## ggplot2 default color scheme
## (equally spaced hues around the color wheel)
x <- scales::hue_pal()(8)
plot_scheme_colorblind(x)
```

## Contributing

Please note that the **khroma** project is released with a [Contributor Code of Conduct](https://www.tesselle.org/conduct.html). By contributing to this project, you agree to abide by its terms.

## References