Use the color scales in this package to make plots that are pretty, better represent your data, easier to read by those with colorblindness, and print well in gray scale.
Install viridis like any R package:
install.packages("viridis")
library(viridis)
For base plots, use the viridis()
function to generate a
palette:
x <- y <- seq(-8*pi, 8*pi, len = 40)
r <- sqrt(outer(x^2, y^2, "+"))
filled.contour(cos(r^2)*exp(-r/(2*pi)),
axes=FALSE,
color.palette=viridis,
asp=1)
For ggplot, use scale_color_viridis()
and
scale_fill_viridis()
:
library(ggplot2)
ggplot(data.frame(x = rnorm(10000), y = rnorm(10000)), aes(x = x, y = y)) +
geom_hex() + coord_fixed() +
scale_fill_viridis() + theme_bw()
viridis
,
and its companion package viridisLite
provide a series of color maps that are designed to improve graph
readability for readers with common forms of color blindness and/or
color vision deficiency. The color maps are also perceptually-uniform,
both in regular form and also when converted to black-and-white for
printing.
These color maps are designed to be:
viridisLite
provides the base functions for generating
the color maps in base R
. The package is meant to be as
lightweight and dependency-free as possible for maximum compatibility
with all the R
ecosystem. viridis
provides additional functionalities, in particular bindings for
ggplot2
.
The package contains eight color scales: “viridis”, the primary choice, and five alternatives with similar properties - “magma”, “plasma”, “inferno”, “civids”, “mako”, and “rocket” -, and a rainbow color map - “turbo”.
The color maps viridis
, magma
,
inferno
, and plasma
were created by Stéfan van
der Walt (@stefanv) and Nathaniel Smith (@njsmith). If you want to know more about
the science behind the creation of these color maps, you can watch this
presentation of
viridis
by their authors at SciPy 2015.
The color map cividis
is a corrected version of
‘viridis’, developed by Jamie R. Nuñez, Christopher R. Anderton, and
Ryan S. Renslow, and originally ported to R
by Marco
Sciaini (@msciain). More info about
cividis
can be found in this
paper.
The color maps mako
and rocket
were
originally created for the Seaborn
statistical data
visualization package for Python. More info about mako
and
rocket
can be found on the Seaborn
website.
The color map turbo
was developed by Anton Mikhailov to
address the shortcomings of the Jet rainbow color map such as false
detail, banding and color blindness ambiguity. More infor about
turbo
can be found here.
Let’s compare the viridis and magma scales against these other commonly used sequential color palettes in R:
rainbow.colors
,
heat.colors
, cm.colors
It is immediately clear that the “rainbow” palette is not perceptually uniform; there are several “kinks” where the apparent color changes quickly over a short range of values. This is also true, though less so, for the “heat” colors. The other scales are more perceptually uniform, but “viridis” stands out for its large perceptual range. It makes as much use of the available color space as possible while maintaining uniformity.
Now, let’s compare these as they might appear under various forms of colorblindness, which can be simulated using the dichromat package: