# Graphing in R for Effective Communication

In which we review graphing options in ggplot2 that allow you to communicate results effectively, including labels, annotations, scales, zooming, and themes.

March 5, 2020 - 9 minute read -

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.

Resources:

#### Overview of Tools:

• Add labels with labs(title = "...", subtitle = "...", caption = "...", x = "...", y = "...")
• Add annotations with geom_text(), geom_label, or ggrepel::geom_label_repel
• Add a reference line with geom_hline(size = 2, color = white) or geom_vline()
• Add a reference rectangle with geom_rect()
• Add a reference arrow with geom_segment() and the arrow argument
• Override or replace default scales with scale_x_continuous() or scale_x_discrete
• Change the legend position with theme(legend.position = "...")
• Change the legend display using guides() with guide_legend() or guide_colorbar()
• Customize color scales with scale_color_brewer(palette = "...") or scale_color_manual(values = ...))
• Zoom in on a region of the plot by setting xlim and ylim in coord_cartesian()
• Save plots with ggsave(), or with knitr in R Markdown

### Labels - Title, Caption, X, and Y Axes

Add labels with labs():

Note: it’s possible to use mathematical notation in your labels by swapping out "" for quote() (see ?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 vjust and hjust:

Reference: For label alignment with hjust and vjust, see below.

• 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
• geom_segment() with arrow argument 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.

Legends:

• Change position of legend using theme(legend.position = "..."); options include "left", "top", "bottom", "right", "none".
• Change display of legend using guides() with guide_legend() or guide_colorbar()

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(), scale_color_gradient_2() (for a diverging scale), scale_fill_gradient(), or 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 xlim and ylim in the coord_cartesian() function.

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 ggthemes.

### Saving Plots

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 = 6 and fig.asp = 0.618 (golden ratio) in defaults, and only adjusting fig.asp in individual chunks.
• Specify output size with out.width. A nice default is out.width = "70%" and fig.align = "center".
• For multiple plots, set out.width and fig.align = "default. To ensure that font size is consistent, every time you adjust out.width, you also need to adjust fig.width to maintain the same ratio with the default out.width
• 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.
1. This post is meant for a person who is looking for a refresher on formatting ggplot2 plots. The content in this post is based on chapter twenty-eight of R for Data Science by Hadley Wickham & Garrett Grolemund.