Skip to contents

Generate colors using the name of a color palette.

Usage

color_palette(x, n = NULL)

Arguments

x

character name of the color palette (see Details for available options).

n

integer number of colors required. A NULL argument can also be used to obtain a default number of colors. If a NULL argument is supplied and the palette has a limited number of colors, then the maximum number of colors is returned. Otherwise, if a NULL argument is supplied and the palette does not have a limited number of colors, then a 20 colors are returned. The default argument is NULL.

Details

This function can be used to generate colors using palettes provided by the RColorBrewer and viridisLite packages. If colors are from multiple palettes, they can be supplied as a single character object delimited by semicolon (";") characters.

Specifically, available palettes include:

Examples

# obtain 5 colors from the Greens palette
color_palette("Greens", 5)
#> [1] "#EDF8E9" "#BAE4B3" "#74C476" "#31A354" "#006D2C"

# obtain all colors from the Greens palette
color_palette("Greens", NULL)
#> [1] "#F7FCF5" "#E5F5E0" "#C7E9C0" "#A1D99B" "#74C476" "#41AB5D" "#238B45"
#> [8] "#006D2C" "#00441B"

# obtain 15 colors using the Set1 and Pastel1 palettes
color_palette("Set1;Pastel1", 15)
#>  [1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00" "#FFFF33" "#A65628"
#>  [8] "#F781BF" "#999999" "#FBB4AE" "#B3CDE3" "#CCEBC5" "#DECBE4" "#FED9A6"
#> [15] "#FFFFCC"

# obtain all colors from the plasma palette
color_palette("plasma", NULL)
#> [1] "#0D0887FF" "#7E03A8FF" "#CC4678FF" "#F89441FF" "#F0F921FF"

# obtain all colors from a random palette
color_palette("random", NULL)
#>  [1] "#543005" "#8C510A" "#BF812D" "#DFC27D" "#F6E8C3" "#F5F5F5" "#C7EAE5"
#>  [8] "#80CDC1" "#35978F" "#01665E" "#003C30"

# obtain 5 colors from random palette(s)
color_palette("random", 5)
#> [1] "#FEF0D9" "#FDCC8A" "#FC8D59" "#E34A33" "#B30000"