Aesthetic specifications

This vignette summarises the various formats that grid drawing functions take. Most of this information is available scattered throughout the R documentation. This appendix brings it all together in one place.

Colour and fill

Almost every geom has either colour, fill, or both. Colours and fills can be specified in the following ways:

Lines

As well as colour, the appearance of a line is affected by linewidth, linetype, linejoin and lineend.

Line type

Line types can be specified with:

Linewidth

Due to a historical error, the unit of linewidth is roughly 0.75 mm. Making it exactly 1 mm would change a very large number of existing plots, so we’re stuck with this mistake.

Line end/join paramters

Mitre joins are automatically converted to bevel joins whenever the angle is too small (which would create a very long bevel). This is controlled by the linemitre parameter which specifies the maximum ratio between the line width and the length of the mitre.

Polygons

The border of the polygon is controlled by the colour, linetype, and linewidth aesthetics as described above. The inside is controlled by fill.

Point

Shape

Shapes take five types of values:

Colour and fill

While colour applies to all shapes, fill only applies to shapes 21-25, as can be seen above. The size of the filled part is controlled by size, the size of the stroke is controlled by stroke. Each is measured in mm, and the total size of the point is the sum of the two. Note that the size is constant along the diagonal in the following figure.

sizes <- expand.grid(size = (0:3) * 2, stroke = (0:3) * 2)
ggplot(sizes, aes(size, stroke, size = size, stroke = stroke)) +
  geom_abline(slope = -1, intercept = 6, colour = "white", linewidth = 6) +
  geom_point(shape = 21, fill = "red") +
  scale_size_identity()

A plot showing a 4-by-4 grid of red points, the top 12 points with black outlines. The size of the points increases horizontally. The stroke of the outlines of the points increases vertically. A white diagonal line with a negative slope marks that the 'stroke' versus 'size' trade-off has similar total sizes.

Because points are not typically filled, you may need to change some default settings when using these shapes and mapping fill. In particular, discrete fill guides will be drawn with an unfilled shape unless overridden (refer to geom_point() for an example of this).

Text

Font family

family sets the typeface of the font. There are only three values that are guaranteed to work everywhere: “sans” (the default), “serif”, or “mono”:

df <- data.frame(x = 1, y = 3:1, family = c("sans", "serif", "mono"))
ggplot(df, aes(x, y)) +
  geom_text(aes(label = family, family = family))

A plot showing three text labels arranged vertically. The top label is 'sans' and is displayed in a sans-serif font. The middle label is 'serif' and is displayed in a serif font. The bottom label is 'mono' and is displayed in a monospaced font.

While these are guaranteed to work, they might map to different typefaces depending on the graphics device and operating system. Choosing any other value puts you at the mercy of the graphics device in use. We strongly recommend using a graphics device built with systemfonts support because this means that you can access all fonts installed on the operating system without any additional work. For now this means using ragg for raster output (PNG, JPEG, TIFF), and svglite for vector output (SVG). See the Fonts from other places section of the systemfonts vignette to learn how to make systemfonts aware of fonts not installed on the computer.

For now there is no PDF device with systemfonts support. If you need to create a PDF file using fonts other than the postscript fonts that pdf() natively support you can look into either of these packages:

Both approaches have pros and cons, so you will to need to try both of them and see which works best for your needs.

Font face

df <- data.frame(x = 1:4, fontface = c("plain", "bold", "italic", "bold.italic"))
ggplot(df, aes(1, x)) +
  geom_text(aes(label = fontface, fontface = fontface))

A plot showing four text labels arranged vertically. The top label is 'bold.italic' and is displayed in bold and italic. The next three labels are 'italic', 'bold' and 'plain' and are displayed in their respective styles.

fontface/face is a catch-all argument that describes the style of the typeface to use. It can take one of 4 values: "plain" is an upright normal-weight font, "italic" is a slanted normal-weight font, "bold" is an upright bold-weight font, and "bold.italic" is a slanted bold-weight font.

The R graphics engine does not allow you to chose other combinations of styles such as other weights or specifying width. See the Extra font styles section of the systemfonts vignette for ways to circumvent this limitation.

Font size

The size of text is measured in mm by default. This is unusual, but makes the size of text consistent with the size of lines and points. Typically you specify font size using points (or pt for short), where 1 pt = 0.35mm. In geom_text() and geom_label(), you can set size.unit = "pt" to use points instead of millimeters. In addition, ggplot2 provides a conversion factor as the variable .pt, so if you want to draw 12pt text, you can also set size = 12 / .pt.

Justification

Horizontal and vertical justification have the same parameterisation, either a string (“top”, “middle”, “bottom”, “left”, “center”, “right”) or a number between 0 and 1:

just <- expand.grid(hjust = c(0, 0.5, 1), vjust = c(0, 0.5, 1))
just$label <- paste0(just$hjust, ", ", just$vjust)

ggplot(just, aes(hjust, vjust)) +
  geom_point(colour = "grey70", size = 5) +
  geom_text(aes(label = label, hjust = hjust, vjust = vjust))

A 3-by-3 grid of text on top of points, with horizontal text justification increasing from 0 to 1 on the x-axis and vertical justification increasing from 0 to 1 on the y-axis. The points make it easier to see the relative placement of text.

Note that you can use numbers outside the range (0, 1), but it’s not recommended.