R Color Cheat Sheet



Data Visualization with ggplot2:: CHEAT SHEET ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same components: a data set, a coordinate system. Leaflet Cheat Sheet an open-source JavaScript library for mobile-friendly interactive maps for GeoJSON and TopoJSON Map Widget Initialization. AddPolygons (color, weight=1, smoothFactor=0.5, opacity=1.0, fillOpacity=0.5.

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…).

Discrete

Barplot

Different type of bar plot

Continuous X, Continuous Y

Scatterplot

Jitter points to account for overlaying points.

Add a rug plot

R Color Cheat Sheet

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.

Add text to a plot

Color

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 Postgresql client dbeaver. 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

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”).

Change Color Palette In R

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:

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.

R Color Codes

R color cheat sheet pdf
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!

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)

Back | Next