Customize diagrams: Formatting


  • link to diagAddElements -> axis(), diagMultiple

Install required packages


Formatting elements for all kinds of diagrams

Nearly all R base diagrams come with a shared set of options to control typical diagram elements like the title, axis labels and limits, or the type of plot symbol. They are illustrated here using the plot() function for simple scatter plots.

Plot symbols and line types

Plot symbols are chosen with option pch.

plot of chunk rerDiagFormat01
plot of chunk rerDiagFormat01

Line types are chosen with option lty, line widths with option lwd, and round vs. square line ends with lend.

plot of chunk rerDiagFormat02
plot of chunk rerDiagFormat02

Diagram title, axis labels, axis limits, aspect ratio

plot of chunk rerDiagFormat03
plot of chunk rerDiagFormat03

Setting parameters with par()

Formatting details of a diagram can also be controlled with a call to par() before a diagram is opened. Some aspects can be set both with par(), and with the diagram function - e.g., with options to plot(). These include (see ?par for explanations):

  • cex, cex.axis, cex.main, cex.lab for the size of plot symbols, axis labels and diagram title
  • col for the color of plot symbols
  • font, family for the font of the diagram annotations
  • las for the orientation of axis labels
  • lend, lty, lwd, pch for the style of lines and plot symbols
  • xaxs, yaxs for the precise axis limits
  • xaxt, yaxt for the presence of axes

Other aspects can only be set in par(). These include: bt, mar, oma, xlog, ylog. The return value of par() is a list with the old value for the changed option. When it is saved, it can later be passed as an option to par() to reset the option for the current graphics device to its previous value.

plot of chunk rerDiagFormat04
plot of chunk rerDiagFormat04


Default palette and other palette choices

[1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta"
[1] "darkviolet"    "ivory"         "snow3"         "mediumorchid3"
[5] "grey97"        "lightsalmon"  
plot of chunk rerDiagFormat05
plot of chunk rerDiagFormat05

RGB colors and transparency

[1] "#00FFFF"
[1] "#FF0000"
[1] "#FF000080"
plot of chunk rerDiagFormat06
plot of chunk rerDiagFormat06

Other color spaces

[1] "#FFFF00"
h 0.500000000
s 1.000000000
v 0.003921569
[1] "#BBDEB1"
[1] "#808080"

Color palettes with RColorBrewer

[1] "#EFF3FF" "#C6DBEF" "#9ECAE1" "#6BAED6" "#3182BD" "#08519C"
 [1] "#EFF3FF" "#E0EAF9" "#D1E1F3" "#C3D9EE" "#B4D3E9" "#A6CDE4" "#96C6DF"
 [8] "#84BCDB" "#72B2D7" "#5EA4D0" "#4994C7" "#3585BE" "#2574B3" "#1662A7"
[15] "#08519C"
plot of chunk rerDiagFormat07
plot of chunk rerDiagFormat07

Useful packages

  • Package colorspace provides more functions for converting between different color spaces. Its documentation illustrates many color palettes for different use cases.
  • The R color chart gives a very nice overview of colors available in R.
  • Package imager can read bitmap images and has many functions for image analysis / manipulation

Detach (automatically) loaded packages (if possible)

Get the article source from GitHub

R markdown - markdown - R code - all posts