R Color Cheat Sheet

Posted on  by 



R has 657 built in color names To see a list of names: colors These colors are displayed on P. R color cheatsheet Finding a good color scheme for presenting data can be challenging. This color cheatsheet will help! R uses hexadecimal to represent colors Hexadecimal is a base-16 number system used to describe color. Color name color name gray8 gray9 gray10 gray11 gray12 gray13 gray14 gray15 gray16 gray17 gray18 gray19 gray20 gray21 gray22 gray23 gray24 gray25 gray26 gray27 gray28. Color Groups RColorBrewer is an R packages that uses the work from to help you choose sensible colour schemes for figures in R. The colors are split into three group, sequential, diverging, and qualitative. Sequential – Light colours for low data, dark for high data. In R, a colour is represented as a string (see Color Specification section of the R par function). Basically, a colour is defined, like in HTML/CSS, using the hexadecimal values (00 to FF) for red, green, and blue, concatenated into a string, prefixed with a '#'. A pure red colour this is represented with '#FF0000'. Predefined Colour Names.

I reproduce some of the plots from Rstudio’s ggplot2 cheat sheet using Base R graphics. I didn’t try to pretty up these plots, but you should.

I use this dataset

The main functions that I generally use for plotting are

  • Plotting Functions
    • plot: Makes scatterplots, line plots, among other plots.
    • lines: Adds lines to an already-made plot.
    • par: Change plotting options.
    • hist: Makes a histogram.
    • boxplot: Makes a boxplot.
    • text: Adds text to an already-made plot.
    • legend: Adds a legend to an already-made plot.
    • mosaicplot: Makes a mosaic plot.
    • barplot: Makes a bar plot.
    • jitter: Adds a small value to data (so points don’t overlap on a plot).
    • rug: Adds a rugplot to an already-made plot.
    • polygon: Adds a shape to an already-made plot.
    • points: Adds a scatterplot to an already-made plot.
    • mtext: Adds text on the edges of an already-made plot.
  • Sometimes needed to transform data (or make new data) to make appropriate plots:
    • table: Builds frequency and two-way tables.
    • density: Calculates the density.
    • loess: Calculates a smooth line.
    • predict: Predicts new values based on a model.

All of the plotting functions have arguments that control the way the plot looks. You should read about these arguments. In particular, read carefully the help page ?plot.default. Useful ones are:

  • main: This controls the title.
  • xlab, ylab: These control the x and y axis labels.
  • col: This will control the color of the lines/points/areas.
  • cex: This will control the size of points.
  • pch: The type of point (circle, dot, triangle, etc…)
  • lwd: Line width.
  • lty: Line type (solid, dashed, dotted, etc…).
Change color palette in r

Discrete

Barplot

Different type of bar plot

Continuous X, Continuous Y

Scatterplot

Jitter points to account for overlaying points.

Add a rug plot

Add a Loess Smoother

Loess smoother with upper and lower 95% confidence bands

Loess smoother with upper and lower 95% confidence bands and that fancy shading from ggplot2.

Kk installer apk. Add text to a plot

Discrete X, Discrete Y

Mosaic Plot

Color code a scatterplot by a categorical variable and add a legend.

par sets the graphics options, where mfrow is the parameter controling the facets.

The first line sets the new options and saves the old options in the list old_options. The last line reinstates the old options.

This R Markdown site was created with workflowr Aruba admin edge port.

R Color Basics

R graphical devices support a broad range of colors and color functions. The colors() command with no input arguments returns a list of all available colors in R. Presently, there are 657 colors. In practice, declaring colors can be achieved three ways:

  • character names (e.g. “red”, “orange”, “yellow”),
  • 3-digit RGB values (e.g. 255 0 0; 265 155 0; 255 255 0), and
  • hexadecimal strings (e.g. “#FF0000”, “#FFA500”, “#FFFF00”).

R Color character names

Index extraction from the colors() list is one way to reveal available colors. For example:

2
4
6
8
10
>colors()[552]
>colors()[grep('orange',colors())]
[1]'darkorange''darkorange1''darkorange2''darkorange3'
[5]'darkorange4''orange''orange1''orange2''orange3'
[14]'orangered3''orangered4'

Using color names in plots is straightforward:

R Color Cheat Sheet

R Color Cheat Sheet Chart

2
4
6
8
10
barplot(1,axes=FALSE,col='steelblue')
# Display all green colors
axes=FALSE,
xlim=c(0,5),
col=colors()[grep('green',colors())])

RGB Color Codes in R

The function col2rgb() can be used to extract the RGB (red-green-blue) components of a color.

2
4
6
col2rgb(c(R='red',O='orange',Y='yellow'))
ROY
green0165255

Each of the RGB color components ranges from 0 to 255. Given that the three RGB components having 256 possible values, then there are 256*256*256 possible colors, or a total of 16,777,216 colors.

Hexadecimal Colors (#rrggbb) in R

Hexadecimal colors represent a more efficient way (for machines) to represent the three RGB colors. Hexadecimal notation is a number scheme with a base of 16 (or hex) and the primary numbers 0, 1, 2 , 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.

For example, a color with the RGB values red=36, green=104, blue=160 is a greyish-blue color. The decimal numbers 36, 104 and 160 are defined in hexadecimal notation as 24, 68 and A0 respectively. To obtain the hex triplet, we simply concatenate the hash symbol and the three hex bytes together: #2468A0. If a number is less than 16 (in decimal notation) or 10 (in hex notation), it must be represented with a leading zero to keep the number of digits in the triplet equal to six. For example, the decimal triplet 0, 1, 2 would be represented by the hex triplet 000102.

R provide the function as.hexmode() to convert decimal numbers into hex numbers and the rgb() function to convert RGB triplets into hex triplets.

R Color Ramps and Hex Triplets

Hex triplets can be used in R in place of color names and are convenient when functions define color selection. For example, it is impossible to use character names to precisely define a color ramp, or the the range of colors that interpolate between a start and end color. Its easy with hex triplets!

R Color Cheat Sheet

R Color Cheat Sheet Template

2
4
6
8
10
# Define the color ramp (returns a function object)
ramp.list<-rgb(ramp(seq(0,1,length=5)),max=255)
[1]'#FFB5C5''#BF87B3''#7F5AA2''#3F2D91''#000080'
# Plot the color ramp
barplot(rep(1,5),axes=FALSE,space=0,col=ramp.list)

R Color Cheat Sheets

Back | Next





Coments are closed