Here, we discuss colors in R, all the named colors in R, colors based on rgb(), gray() and color palettes like the rainbow() function.

All functions are from the "grDevices" package.

You can set color in graphs and plots with the argument col, for example:

col = "blue"
col = colors()[366]
col = rgb(0.5, 1, 0)
col = "#80FF00"

See also setting colors & fonts and plots & charts to see how colors are used on different plots and charts.

1 List of All the Colors in R

There are 657 named colors in R. They can be viewed with the entry below in your console:

colors()

Or select specific numbers in the figure below to view specific color names:

colors()[1]
[1] "white"
colors()[366]
[1] "honeydew4"
colors()[632]
[1] "tomato2"

The figure below shows all the colors from Color 1 to Color 657.

Figure Showing All the Colors in R

Figure Showing All the Colors in R

Also, the paginated table below lists all the colors from Color 1 to Color 657. You can filter the table by color name or color number range.

2 Rainbow Colors in R

For easy expression of colors in R, you can use one of the many color pallets available which includes the rainbow palette.

To derive 4 or 8 colors for example, you simply use the rainbow() function with argument 4 or 8 respectively. Hence, instead of listing 3 colors as in col = c("tan", "orange", "violet"), use rainbow(3) if you are fine with any 3 different colors.

rainbow(4)
[1] "#FF0000" "#80FF00" "#00FFFF" "#8000FF"
rainbow(8)
[1] "#FF0000" "#FFBF00" "#80FF00" "#00FF40" "#00FFFF" "#0040FF" "#8000FF"
[8] "#FF00BF"
rainbow(8)[1:4]
[1] "#FF0000" "#FFBF00" "#80FF00" "#00FF40"
rainbow(8)[5:8]
[1] "#00FFFF" "#0040FF" "#8000FF" "#FF00BF"

The appearance of these colors can be seen in the figure below.

Colors from the Rainbow Palette in R

Colors from the Rainbow Palette in R

3 Gray Colors in R

For gray scale colors, you can use the gray() function. It takes values between 0 and 1, with 0 meaning "black" and 1 meaning "white".

To derive 4 colors for example, you simply use gray((1:4)/4) or gray(c(1/4, 2/4, 3/4, 4/4)).

gray((1:4)/4)
[1] "#404040" "#808080" "#BFBFBF" "#FFFFFF"
gray(c(1/4, 2/4, 3/4, 4/4))
[1] "#404040" "#808080" "#BFBFBF" "#FFFFFF"

The appearance of these colors can be seen in the figure below.

Gray Scale Colors in R

Gray Scale Colors in R

4 RGB Colors in R

For the RGB color specification, you can use the rgb() function. It takes values between 0 and 1, for each of "red", "green", and "blue" respectively.

To derive a color for example, you simply use rgb(0.5, 1, 0). rgb(1, 0, 0) = "red", rgb(0, 1, 0) = "green", rgb(0, 0, 1) = "blue".

rgb(0.5, 1, 0)
[1] "#80FF00"

The appearance of examples from color specifications of this format can be seen in the figure below.

RGB Format Colors in R

RGB Format Colors in R

5 Color Palettes in R

Here we present color palettes in R which includes, rainbow(), cm.colors(), hcl.colors(), heat.colors(), terrain.colors() and topo.colors().

These are done in pie chart formats.

The figures below show what 3, 8, and 12 colors (for example rainbow(3)) from these palettes look like in R.

rainbow(3)
[1] "#FF0000" "#00FF00" "#0000FF"

3 colors:

3 Colors from Color Palettes in R

3 Colors from Color Palettes in R

8 colors:

8 Colors from Color Palettes in R

8 Colors from Color Palettes in R

12 colors:

12 Colors from Color Palettes in R

12 Colors from Color Palettes in R

Transparency of Color Palettes in R:

The palettes, rgb() and gray() all have transparency option which is specified with the "alpha" argument. For example rainbow(12, alpha = 0).

rainbow(12, alpha = 0)
 [1] "#FF000000" "#FF800000" "#FFFF0000" "#80FF0000" "#00FF0000" "#00FF8000"
 [7] "#00FFFF00" "#0080FF00" "#0000FF00" "#8000FF00" "#FF00FF00" "#FF008000"

It takes values between 0 and 1. Here, 0 means fully transparent and 1 means opaque, with the default value equal to 1.

Transparency of Colors Palettes in R

Transparency of Colors Palettes in R

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes