Create beautiful color maps with Viridis


The viridis package in R significantly enhances data visualization by offering a collection of color maps that are perceptually uniform in color and brightness. Crafted with the needs of colorblind users in mind, this package features color scales that maintain clarity and consistency even when viewed in grayscale.

Documentation

{Viridis}

Quick start


The viridis package in R is an extension of the ggplot2 package, designed to simplify the process of creating visually appealing color maps. It offers a set of color maps that are perceptually uniform in color and brightness, making them ideal for use in data visualization.

✍️ author → Simon Garnier

📘 documentationGithub

⭐️ more than 250 stars on github

Installation


To get started with viridis, you can install it directly from CRAN using the install.packages function:

install.packages("viridis")

Basic usage


The viridis package is very easy to use. Start by loading it with library(viridis), and then you can use the color palettes in your plots!

library(viridis)
colors = viridis(3)

# basic plot
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 6) +
  scale_color_manual(values = colors) +
  theme_minimal()

Key features


→ Combine ggplot2 and Viridis

The viridis package is an extension of the ggplot2 package, which means you can use it to customize the color of your plots.

It provides scale_color_viridis() and scale_fill_viridis() functions that allow you to use the viridis color palettes in your plots.

Example:

# load libraries
library(viridis)
library(ggplot2)

# create dataframe
dsub <- subset(diamonds, x > 5 & x < 6 & y > 5 & y < 6)
dsub$diff <- with(dsub, sqrt(abs(x - y)) * sign(x - y))

# create plot with Turbo color palette
ggplot(dsub, aes(x, y, colour = diff)) +
  geom_point() +
  scale_color_viridis(option='turbo') +
  theme_minimal()


→ Beautiful filled.contour

The filled.contour() function is a base R function that creates a filled contour plot. You can use the color.palette argument to specify a color palette from the viridis package.

Example:

library(viridis)
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=magma,
  asp=1
)


→ Use Viridis with lattice

The viridis package can be used with the lattice package to create beautiful plots. You can use the colorkey argument to specify a color palette from the viridis package.

Example:

library(viridis)
library(lattice)
levelplot(volcano, col.regions = mako(100))





❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥