The tools covered in this post are important for communicating information to an audience that may not be as familiar with the data that you are working with.1 I’ll first give a short overview of the most common functions used with graphs before diving into examples of each.
Overview of Tools:
- Add labels with
labs(title = "...", subtitle = "...", caption = "...", x = "...", y = "...")
- Add annotations with
- Add a reference line with
geom_hline(size = 2, color = white)or
- Add a reference rectangle with
- Add a reference arrow with
- Override or replace default scales with
- Change the legend position with
theme(legend.position = "...")
- Change the legend display using
- Customize color scales with
scale_color_brewer(palette = "...")or
scale_color_manual(values = ...))
- Zoom in on a region of the plot by setting
- Save plots with
ggsave(), or with
knitrin R Markdown
Labels - Title, Caption, X, and Y Axes
Add labels with
Note: it’s possible to use mathematical notation in your labels by
?plotmath for details)
Annotations - Writing information directly onto the plot
Add annotations with
ggrepel::geom_label_repel to notate certain points on a plot using labels that are guaranteed not to overlap with each other:
Add a single label to the graph by manually specifying the label text and location in a data frame, and specifying text alignment with
Reference: For label alignment with
vjust, see below.
Additional annotation options include:
geom_hline(size = 2, color = white)and
geom_vline()add reference lines
geom_rect()draws a rectangle around points of interest, with boundaries defined by aes:
xmin, xmax, ymin, ymax
arrowargument to draw an arrow, and define locations with aes:
x, y, xend, yend
Scales - Axis ticks and Legend Formatting
Scales are added by default for each mapping function, but you might want to override defaults to tweak parameters such as the breaks on axes or labels on the legend, or replace the scale altogether and use a different algorithm.
If you want to only highlight exactly where observations occur, set the breaks as a variable in the data. For example, the code and graph of U.S. presidents’ serving terms below only displays the years signifying the start of a new presidential term.
- Change position of legend using
theme(legend.position = "..."); options include
"left", "top", "bottom", "right", "none".
- Change display of legend using
Customizing color scales:
scale_color_brewer(palette = "Set1")changes discrete colors to the ColorBrewer scales for colorblindness.
scale_color_manual(values = c(Republican = "red", Democratic = "blue"))manually maps values and colors.
- For continuous colors, use
scale_color_gradient_2()(for a diverging scale),
scale_color_viridis()(continuous analog to ColorBrewer scales).
Zooming: A closer look at the data
To zoom in on a region of the plot, set
ylim in the
Another useful tool is to match limits across multiple subplots using the limits of the entire datset. This way, you can more easily compare different subsets of the data.
Themes: Alter the look of the plot
Themes change the way that the non-data elements of your graph looks. R has 8 default themes, shown below, and more in add-on packages like
Save plots with
ggsave(), or with
knitr in R Markdown. In R Markdown, you can control figure sizing in the folowing ways:
- Make plots a consistent width by setting
fig.width = 6and
fig.asp = 0.618(golden ratio) in defaults, and only adjusting
fig.aspin individual chunks.
- Specify output size with
out.width. A nice default is
out.width = "70%"and
fig.align = "center".
- For multiple plots, set
fig.align = "default. To ensure that font size is consistent, every time you adjust
out.width, you also need to adjust
fig.widthto maintain the same ratio with the default
- To add captions, use
fig.cap. This will also change the igure from inline to “floating”
- If producing PDF output with very large plots, set
dev = "png"to force using PNGs for speed and size.
- The name of chunks is used to generate the file name of the graphic on disk.