Appendix L — Styling Plots

L.1 Aesthetics

L.1.1 Colour/Fill

The colour argument changes the point and line colour, while the fill argument changes the interior colour of shapes. Type colours() into the console to see a list of all the named colours in R. Alternatively, you can use hexadecimal colours like "#FF8000" or the rgb() function to set red, green, and blue values on a scale from 0 to 1.

Hover over a colour to see its R name.

  • black
  • gray1
  • gray2
  • gray3
  • gray4
  • gray5
  • gray6
  • gray7
  • gray8
  • gray9
  • gray10
  • gray11
  • gray12
  • gray13
  • gray14
  • gray15
  • gray16
  • gray17
  • gray18
  • gray19
  • gray20
  • gray21
  • gray22
  • gray23
  • gray24
  • gray25
  • gray26
  • gray27
  • gray28
  • gray29
  • gray30
  • gray31
  • gray32
  • gray33
  • gray34
  • gray35
  • gray36
  • gray37
  • gray38
  • gray39
  • gray40
  • dimgray
  • gray42
  • gray43
  • gray44
  • gray45
  • gray46
  • gray47
  • gray48
  • gray49
  • gray50
  • gray51
  • gray52
  • gray53
  • gray54
  • gray55
  • gray56
  • gray57
  • gray58
  • gray59
  • gray60
  • gray61
  • gray62
  • gray63
  • gray64
  • gray65
  • darkgray
  • gray66
  • gray67
  • gray68
  • gray69
  • gray70
  • gray71
  • gray72
  • gray73
  • gray74
  • gray
  • gray75
  • gray76
  • gray77
  • gray78
  • gray79
  • gray80
  • gray81
  • gray82
  • gray83
  • lightgray
  • gray84
  • gray85
  • gainsboro
  • gray86
  • gray87
  • gray88
  • gray89
  • gray90
  • gray91
  • gray92
  • gray93
  • gray94
  • gray95
  • gray96
  • gray97
  • gray98
  • gray99
  • white
  • snow4
  • snow3
  • snow2
  • snow
  • rosybrown4
  • rosybrown
  • rosybrown3
  • rosybrown2
  • rosybrown1
  • lightcoral
  • indianred
  • indianred4
  • indianred2
  • indianred1
  • indianred3
  • brown4
  • brown
  • brown3
  • brown2
  • brown1
  • firebrick4
  • firebrick
  • firebrick3
  • firebrick1
  • firebrick2
  • darkred
  • red3
  • red2
  • red
  • mistyrose3
  • mistyrose4
  • mistyrose2
  • mistyrose
  • salmon
  • tomato3
  • coral4
  • coral3
  • coral2
  • coral1
  • tomato2
  • tomato
  • tomato4
  • darksalmon
  • salmon4
  • salmon3
  • salmon2
  • salmon1
  • coral
  • orangered4
  • orangered3
  • orangered2
  • lightsalmon3
  • lightsalmon2
  • lightsalmon
  • lightsalmon4
  • sienna
  • sienna3
  • sienna2
  • sienna1
  • sienna4
  • orangered
  • seashell4
  • seashell3
  • seashell2
  • seashell
  • chocolate4
  • chocolate3
  • chocolate
  • chocolate2
  • chocolate1
  • linen
  • peachpuff4
  • peachpuff3
  • peachpuff2
  • peachpuff
  • sandybrown
  • tan4
  • peru
  • tan2
  • tan1
  • darkorange4
  • darkorange3
  • darkorange2
  • darkorange1
  • antiquewhite3
  • antiquewhite2
  • antiquewhite1
  • bisque4
  • bisque3
  • bisque2
  • bisque
  • burlywood4
  • burlywood3
  • burlywood
  • burlywood2
  • burlywood1
  • darkorange
  • antiquewhite4
  • antiquewhite
  • papayawhip
  • blanchedalmond
  • navajowhite4
  • navajowhite3
  • navajowhite2
  • navajowhite
  • tan
  • floralwhite
  • oldlace
  • wheat4
  • wheat3
  • wheat2
  • wheat
  • wheat1
  • moccasin
  • orange4
  • orange3
  • orange2
  • orange
  • goldenrod
  • goldenrod1
  • goldenrod4
  • goldenrod3
  • goldenrod2
  • darkgoldenrod4
  • darkgoldenrod
  • darkgoldenrod3
  • darkgoldenrod2
  • darkgoldenrod1
  • cornsilk
  • cornsilk4
  • cornsilk3
  • cornsilk2
  • lightgoldenrod4
  • lightgoldenrod3
  • lightgoldenrod
  • lightgoldenrod2
  • lightgoldenrod1
  • gold4
  • gold3
  • gold2
  • gold
  • lemonchiffon4
  • lemonchiffon3
  • lemonchiffon2
  • lemonchiffon
  • palegoldenrod
  • khaki
  • darkkhaki
  • khaki4
  • khaki3
  • khaki2
  • khaki1
  • ivory4
  • ivory3
  • ivory2
  • ivory
  • beige
  • lightyellow4
  • lightyellow3
  • lightyellow2
  • lightyellow
  • lightgoldenrodyellow
  • yellow4
  • yellow3
  • yellow2
  • yellow
  • olivedrab
  • olivedrab4
  • olivedrab3
  • olivedrab2
  • olivedrab1
  • darkolivegreen
  • darkolivegreen4
  • darkolivegreen3
  • darkolivegreen2
  • darkolivegreen1
  • greenyellow
  • chartreuse4
  • chartreuse3
  • chartreuse2
  • lawngreen
  • chartreuse
  • honeydew4
  • honeydew3
  • honeydew2
  • honeydew
  • darkseagreen4
  • darkseagreen
  • darkseagreen3
  • darkseagreen2
  • darkseagreen1
  • lightgreen
  • palegreen
  • palegreen4
  • palegreen3
  • palegreen1
  • forestgreen
  • limegreen
  • darkgreen
  • green4
  • green3
  • green2
  • green
  • mediumseagreen
  • seagreen
  • seagreen3
  • seagreen2
  • seagreen1
  • mintcream
  • springgreen4
  • springgreen3
  • springgreen2
  • springgreen
  • aquamarine3
  • aquamarine2
  • aquamarine
  • mediumspringgreen
  • aquamarine4
  • turquoise
  • mediumturquoise
  • lightseagreen
  • azure4
  • azure3
  • azure2
  • azure
  • lightcyan4
  • lightcyan3
  • lightcyan2
  • lightcyan
  • paleturquoise
  • paleturquoise4
  • paleturquoise3
  • paleturquoise2
  • paleturquoise1
  • darkslategray
  • darkslategray4
  • darkslategray3
  • darkslategray2
  • darkslategray1
  • cyan4
  • cyan3
  • darkturquoise
  • cyan2
  • cyan
  • cadetblue4
  • cadetblue
  • turquoise4
  • turquoise3
  • turquoise2
  • turquoise1
  • powderblue
  • cadetblue3
  • cadetblue2
  • cadetblue1
  • lightblue4
  • lightblue3
  • lightblue
  • lightblue2
  • lightblue1
  • deepskyblue4
  • deepskyblue3
  • deepskyblue2
  • deepskyblue
  • skyblue
  • lightskyblue4
  • lightskyblue3
  • lightskyblue2
  • lightskyblue1
  • lightskyblue
  • skyblue4
  • skyblue3
  • skyblue2
  • skyblue1
  • aliceblue
  • slategray
  • lightslategray
  • slategray3
  • slategray2
  • slategray1
  • steelblue4
  • steelblue
  • steelblue3
  • steelblue2
  • steelblue1
  • dodgerblue4
  • dodgerblue3
  • dodgerblue2
  • dodgerblue
  • lightsteelblue4
  • lightsteelblue3
  • lightsteelblue
  • lightsteelblue2
  • lightsteelblue1
  • slategray4
  • cornflowerblue
  • royalblue
  • royalblue4
  • royalblue3
  • royalblue2
  • royalblue1
  • ghostwhite
  • lavender
  • midnightblue
  • navy
  • blue4
  • blue3
  • blue2
  • blue
  • darkslateblue
  • slateblue
  • mediumslateblue
  • lightslateblue
  • slateblue1
  • slateblue4
  • slateblue3
  • slateblue2
  • mediumpurple4
  • mediumpurple3
  • mediumpurple
  • mediumpurple2
  • mediumpurple1
  • purple4
  • purple3
  • blueviolet
  • purple1
  • purple2
  • purple
  • darkorchid
  • darkorchid4
  • darkorchid3
  • darkorchid2
  • darkorchid1
  • darkviolet
  • mediumorchid4
  • mediumorchid3
  • mediumorchid
  • mediumorchid2
  • mediumorchid1
  • thistle4
  • thistle3
  • thistle
  • thistle2
  • thistle1
  • plum4
  • plum3
  • plum2
  • plum1
  • plum
  • violet
  • darkmagenta
  • magenta3
  • magenta2
  • magenta
  • orchid4
  • orchid3
  • orchid
  • orchid2
  • orchid1
  • maroon4
  • violetred
  • maroon3
  • maroon2
  • maroon1
  • mediumvioletred
  • deeppink3
  • deeppink2
  • deeppink
  • deeppink4
  • hotpink2
  • hotpink1
  • hotpink4
  • hotpink
  • violetred4
  • violetred3
  • violetred2
  • violetred1
  • hotpink3
  • lavenderblush4
  • lavenderblush3
  • lavenderblush2
  • lavenderblush
  • maroon
  • palevioletred4
  • palevioletred3
  • palevioletred
  • palevioletred2
  • palevioletred1
  • pink4
  • pink3
  • pink2
  • pink1
  • pink
  • lightpink
  • lightpink4
  • lightpink3
  • lightpink2
  • lightpink1

L.1.2 Alpha

The alpha argument changes transparency (0 = totally transparent, 1 = totally opaque).

A demo with 'alpha' on the x-axis and ticks from 0.0 to 1.0 in 0.1 steps. The top line shows blue dota with increasing alpha (i.e., decreacing transparency) over a black line. The bottom line shows black dots with increasing alpha over a blue line.
Figure L.1: Varying alpha values

L.1.3 Shape

The shape argument changes the shape of points.

The numbers 1-25 on the x-axis, with the 25 different point shapes above them: open cirlce, open up triangle, +, x, open diamond, open down triangle, box with x, *, diamond with +, circle with +, star of david, box with +, circle with x, box with up triangle, solid square, solid circle, solid up triangle, solid diamond, big solid circle, small solid circle, open circle, open square, open diamond, open up triangle, open down triangle
Figure L.2: The 25 shape values

L.1.4 Linetype

You can probably guess what the linetype argument does.

The x-axis shows linetype, with 6 values (1-6). There are 4 facets of different linewidths (0.25, 0.5, 1, 2). Linetype 1 is solid, 2 is dashes, 3 is dots, 4 is alternating dot dash, 5 is long dashes, and 6 is longer dot dash.
Figure L.3: The 6 linetype values at different sizes

L.2 Palettes

Discrete palettes change depending on the number of categories.

A series of violin plots showing 1 to 8 violins and the colours that are used for each combination.
Figure L.4: Default discrete palette with different numbers of levels.

L.2.1 Viridis Palettes

Viridis palettes are very good for colourblind-safe and greyscale-safe plots. The work with any number of categories, but are best for larger numbers of categories or continuous colours.

L.2.1.1 Discrete Viridis Palettes

Set discrete viridis colours with scale_colour_viridis_d() or scale_fill_viridis_d() and set the option argument to one of the options below. Set direction = -1 to reverse the order of colours.

5 rows and 2 columns of violin plots. Each row shows a different colour palette from the viridis options: magma, inferno, plasma, viridis, and cividis. The left plot in each row shows the colours for 5 levels, the right plot shows the colours for 15 levles.
Figure L.5: Discrete viridis palettes
Note

If the end colour is too light for your plot or the start colour too dark, you can set the begin and end arguments to values between 0 and 1, such as scale_colour_viridis_c(begin = .1, end = .9).

L.2.1.2 Continuous Viridis Palettes

Set continuous viridis colours with scale_colour_viridis_c() or scale_fill_viridis_c() and set the option argument to one of the options below. Set direction = -1 to reverse the order of colours.

Five hex plots showing density across the x and y axes, where lighter colours correspond to higher count.  Each panel shows a different colour palette from the viridis options: magma, inferno, plasma, viridis, and cividis.
Figure L.6: Continuous viridis palettes.

L.2.2 Brewer Palettes

Brewer palettes give you a lot of control over plot colour and fill. You set them with scale_color_brewer() or scale_fill_brewer() and set the palette argument to one of the palettes below. Set direction = -1 to reverse the order of colours.

L.2.2.1 Qualitative Brewer Palettes

These palettes are good for categorical data with up to 8 categories (some palettes can handle up to 12). The “Paired” palette is useful if your categories are arranged in pairs.

8 violin plots showing the 8-12 colours in each palette (numebr of colours in parentheses): Accent (8), Dark2 (8), Paired (12), Pastel1 (8), Pastel2 (8), Set1 (9), Set2 (8), Set3 (12)
Figure L.7: Qualitative brewer palettes.

L.2.2.2 Sequential Brewer Palettes

These palettes are good for up to 9 ordinal categories with a lot of categories.

18 violin plots showing the 9 colours in each palette: Reds, RdPu, Oranges, OrRd, YlOrRd, YlOrBr, YlGn, YlGnBu, Greens, GnBu, Blues, BuGn, BuPu, Purples, PuRd, PuBu, PuBuGn, Greys
Figure L.8: Sequential brewer palettes.

L.2.2.3 Diverging Brewer Palettes

These palettes are good for ordinal categories with up to 11 levels where the centre level is a neutral or baseline category and the levels above and below it differ in an important way, such as agree versus disagree options.

Nine violin plots showing the 11 colours in each palette: BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, and Spectral
Figure L.9: Diverging brewer palettes.

L.3 Themes

ggplot2 has 8 built-in themes that you can add to a plot like plot + theme_bw() or set as the default theme at the top of your script like theme_set(theme_bw()).

The same plot in 8 different themes: bw, classic, dark, gray, light, linedraw, minimal, void
Figure L.10: {ggplot2} themes.

L.3.1 ggthemes

You can get more themes from add-on packages, like ggthemes”, “https://yutannihilation.github.io/allYourFigureAreBelongToUs/ggthemes/”). Most of the themes also have customscale_functions likescale_colour_economist()`. Their website has extensive examples and instructions for alternate or dark versions of these themes.

The same plot in 17 different themes: calc, economist, economist_white, excel, excel_new, few, fivethirtyeight, gdocs, hc, igray, map, pander, solarized, solarized_2, stata, tufte, and wsj.
Figure L.11: {ggthemes} themes.

L.3.2 Fonts

You can customise the fonts used in themes. All computers should be able to recognise the families “sans”, “serif”, and “mono”, and some computers will be able to access other installed fonts by name.

The same plot with the stystem standard Sans, Serif, Mono, fonts, plus Comic Sans MS.
Figure L.12: Different fonts.
Warning

If you are working on a Windows machine and get the error “font family not found in Windows font database”, you may need to explicitly map the fonts. In your setup code chunk, add the following code, which should fix the error. You may need to do this for any fonts that you specify.

The showtext package is a flexible way to add fonts.

If you have a .ttf file from a font site, like Font Squirrel, you can load the file directly using font_add(). Set regular as the path to the file for the regular version of the font, and optionally add other versions. Set the family to the name you want to use for the font. You will need to include any local font files if you are sharing your script with others.

To download fonts directly from Google fonts, use the function font_add_google(), set the name to the exact name from the site, and the family to the name you want to use for the font.

After you’ve added fonts from local files or Google, you need to make them available to R using showtext_auto(). You will have to do these steps in each script where you want to use the custom fonts.

To change the fonts used overall in a plot, use the theme() function and set text to element_text(family = "new_font_family").

The same plot with three fonts: Courgette (italic handwriting), Cartoonist Hand (cartoon style), and Poiret One (thin art deco).
Figure L.13: Custom Fonts.

To set the fonts for individual elements in the plot, you need to find the specific argument for that element. You can use the argument face to choose “bold”, “italic”, or “bolditalic” versions, if they are available.

The same plot with the title in Cartoonist Hand bold, the facet strip titles in cartoonist hand italic, and the x-axis labels in sans serif font.
Figure L.14: Multiple custom fonts on the same plot.