viridis_scale: Viridis color scale and palette

View source: R/scale_viridis.R

viridis_scaleR Documentation

Viridis color scale and palette

Description

The viridis color palette, by Nathaniel J. Smith, Stefan van der Walt, and Eric Firing (CC0 license).

Usage

viridis_scale(
  domain = c(0, 1),
  reverse = FALSE,
  na.value = NULL,
  extrapolate = FALSE
)

viridis_map(x, ...)

viridis_palette(...)

viridis_colors(n, ...)

scale_color_viridis_c(
  ...,
  reverse = FALSE,
  na.value = NULL,
  guide = "colorbar"
)

scale_fill_viridis_c(..., reverse = FALSE, na.value = NULL, guide = "colorbar")

scale_color_viridis_d(..., reverse = FALSE, na.value = NULL, guide = "legend")

scale_fill_viridis_d(..., reverse = FALSE, na.value = NULL, guide = "legend")

Arguments

domain

vector of two values between which the scale is computed.

reverse

whether to reverse the order of colors along the scale.

na.value

value to return for missing values in the input. Can be either a color, NULL in which case a tentitatively appropriate color will be chosen automatically, or NA.

extrapolate

when FALSE, the default, return NA for input values that are out of the domain; when TRUE return the color corresponding to the extreme of the domain instead.

x

a vector whose values will be coerced to numbers and mapped to colors.

...

passed to viridis_scale from other viridis_* functions; passed to ggplot2::continuous_scale or ggplot2::discrete_scale from the scale_* functions, as appropriate. NB: in all situations, passing domain is meaningless and yields an error.

n

number of colors to extract from the color palette.

guide

type of guide for the legend ("colorbar" for a continuous colorbar, "legend" for a categorical guide) or guide object itself.

Value

*_scale returns a function. This function takes a single argument (x: a numeric vector), maps its values to colors, and returns thee colors as hex codes.

*_map is a shortcut for *_scale(domain=range(x))(x): it creates a scale that spans the range of values in argument x, maps the content of x on that scale, and returns the colors.

*_palette returns a function. This function takes an integer (n) as argument, picks n colors evenly spaced along the scale, and returns them as hex codes.

*_colors is a shortcut for *_palette()(n) and directly returns n evenly spaced colors. It is equivalent to built-in functions such as heat.colors, topo.colors, etc.

scale_* return a ggplot2 scale, either discrete (similar to scale_color_discrete) or continuous (similar to scale_color_continuous).

See Also

viridis for the colors in the palette.

Other color scales and palettes: brewer_scale(), chroma_scale(), cubehelix_scale(), etopo_scale(), hue_scale(), inferno_scale(), interp_scale(), light_scale(), magma_scale(), plasma_scale(), turbo_scale(), wikitopo_scale()

Examples

# Get a few colors along the palette
show_col(
  viridis_palette()(20),
  viridis_colors(50),
  viridis_colors(20, reverse=TRUE)
)

# 1/ Represent a continuous variable

# Map the elevation of the Maunga Whau volcano
image(maunga, col=viridis_colors(100), asp=1)
contour(maunga, col=alpha("white", 0.5), add=TRUE)

persp(maunga, theta=50, phi=25, scale=FALSE, expand=2,
      border=alpha("black", 0.4),
      col=viridis_map(persp_facets(maunga$z)))

## Not run: 
# with ggplot2
library("ggplot2")
ggplot(maungaxyz) + coord_fixed() +
  geom_raster(aes(x=x, y=y, fill=z)) +
  geom_contour(aes(x=x, y=y, z=z), color="white", alpha=0.5) +
  scale_fill_viridis()

# in spinning 3D
library("rgl")
persp3d(maunga, aspect=c(1,0.7,0.2), axes=FALSE, box=FALSE,
        col=viridis_map(maunga$z))
play3d(spin3d(axis=c(0, 0, 1), rpm=10), duration=6)
## End(Not run)

# Represent a third variable on a scatterplot
attach(airquality)
# define a scale encompassing the whole data
my_scale <- viridis_scale(domain=c(0,200))
# use the same scale for the plot and the legend
pars <- sidemargin()
plot(Wind, Temp, col=my_scale(Ozone), pch=19)
sidelegend(legend=c(pretty(Ozone), "NA"),
           col=my_scale(c(pretty(Ozone), NA)), pch=19)
par(pars)

## Not run: 
# or with ggplot2
# but the light yellows at the top of the scale are difficult to see
# on points; either outline them or put them on a dark background
ggplot(airquality) +
  geom_point(aes(x=Wind, y=Temp, fill=Ozone), shape=21, size=2) +
  scale_fill_viridis()
ggplot(airquality) + theme_dark() +
  geom_point(aes(x=Wind, y=Temp, color=Ozone)) +
  scale_color_viridis(na.value="grey60")
## End(Not run)


# 2/ Represent a discrete variable
# albeit only with a limited number of levels

attach(iris)
pars <- sidemargin()
plot(Petal.Length, Petal.Width, pch=21, bg=viridis_map(Species))
sidelegend(legend=levels(Species),
           pt.bg=viridis_colors(n=nlevels(Species)), pch=21)
par(pars)

## Not run: 
# or with ggplot2
ggplot(iris) +
  geom_point(aes(Petal.Length, Petal.Width, fill=Species), shape=21) +
  scale_fill_viridis_d()
## End(Not run)

jiho/chroma documentation built on Nov. 26, 2022, 2:39 a.m.