# packages and global themelibrary(ggplot2)library(dplyr)theme_set(theme_bw(base_size = 14))
<- iris %>% ggplot(aes(Sepal.Length, Sepal.Width)) base
+ base geom_jitter(aes(color = Species), size = 3) + # Species a categorical variable scale_color_viridis_d(option = "D") # use suffix 'd' for discrete color scale
A Complete Guide on the Viridis Color Palettes

In the former tutorial, we covered in detail the brewer palettes. In this tutorial, we’ll discuss the pretty viridis color palettes (shown above), which shares a similar syntax with the brewer palettes.
- Three types of scales:
scale_color_viridis_d
,c
, andb
- Extract color hex codes from the viridis palettes
- Display colors from the viridis palettes
scale_color_viridis_*()
with suffix d
, c
, or b
The basic formula of a viridis color scale is scale_color_viridis_*()
, with *
referring to three suffix options, including d
, c
, and b
, depending on the type of the variable that is mapped to the color
aesthetic. The palette is selected using argument e.g. option = "C"
or option = "plasma"
(see names and associated alphabets above).
- When a categorical variable is mapped to
color
, usescale_color_viridis_d
to create discrete color legends.
When a numerical variable is mapped to color
:
- use
scale_color_viridis_c
to generate continuous color bar, or - use
scale_color_viridis_b
to create binned (stepwise) color scale.
+ base # Petal.Length a numeric variable geom_jitter(aes(color = Petal.Length), size = 3) + # use suffix 'c' for continuous color scale scale_color_viridis_c(option = "D")
+ base geom_jitter(aes(color = Petal.Length), size = 3) + # use suffix "b" for binned color legend scale_color_viridis_b(option = "D")
In like manner,
scale_fill_viridis_*()
is used for thefill
aesthetic, with the suffix options following the same rules above.
Extract colors from viridis palettes
The above scale_color_viridis_*()
functions are built-in ggplot2, and are directly available once the package ggplot2 is loaded. However, in order to extract color hex codes from the viridis palettes, you need to additionally load the viridis
package.
library(viridis)# extract 20 colors from palette option A<- viridis(20, option = "A") a a
Output:
[1] "#000004FF" "#07071DFF" "#160F3BFF" "#29115AFF" "#400F73FF" "#56147DFF"
[7] "#6B1D81FF" "#802582FF" "#952C80FF" "#AB337CFF" "#C03A76FF" "#D6456CFF"
[13] "#E85362FF" "#F4685CFF" "#FA815FFF" "#FD9A6AFF" "#FEB37BFF" "#FECC8FFF"
[19] "#FDE4A6FF" "#FCFDBFFF"
Unlike the brewer palettes which are subjected to the maximum color number limits, the viridis palette has no such limit; it allows an unlimited number of transitional colors to be automatically interpolated and extracted.
Use the scales
package to visualize the color hex codes.
library(scales)show_col(a, cex_label = .6) # use 'cex_label' to set label size
Display colors from the viridis palettes
There is no simple function to display the color scales in an easy way. However, you can plot out the palettes using the following script. The script is contributed by Prof. José María Sallan Leyes at Universitat Politècnica de Catalunya.
<-c( viridis_names "A (magma)", "B (inferno)", "C (plasma)", "D (viridis)", "E (cividis)", "F (rocket)", "G (mako)", "H (turbo)")
<- 50 # control the smoothness of color transition n par(mfrow=c(4,2), mar = rep(1, 4))
<- sapply(1:8, function(x) image( f matrix(1:n, n, 1), col = viridis(n = n, option = LETTERS[x]), axes =FALSE, main = viridis_names[x], cex.main = 1.5) )