Visualization

Supplemental Visualization

Combining plots using Patchwork

A good alternative to using two y-axes is to use two plots instead. The patchwork package makes this really easy to do with R. There are other similar packages that do this, like cowplot and gridExtra, but I’ve found that patchwork is the easiest to use and it actually aligns the different plot elements like axis lines and legends. The documentation for patchwork is really great and full of examples—you should check it out to see all the things you can do with it!

First, we load the libraries and data we’ll be using. We loaded the Atlanta weather data in Example 05:

library(tidyverse)  # For ggplot, dplyr, and friends
library(patchwork)  # For combining ggplot plots
weather_atl <- read_csv("/data/atl-weather-2019.csv")

To use **patchwork**, we need to (1) save our plots as objects and (2) add them together with `+`.

For instance, is there a relationship between temperature and humidity in Atlanta? We can plot both:

::: {.cell}

```{.r .cell-code}
# Temperature in Atlanta
temp_plot <- ggplot(weather_atl, aes(x = time, y = temperatureHigh)) +
  geom_line() +
  geom_smooth() +
  scale_y_continuous(sec.axis = sec_axis(trans = ~ (32 - .) * -5/9,
                                         name = "Celsius")) +
  labs(x = NULL, y = "Fahrenheit") +
  theme_minimal()
temp_plot

# Humidity in Atlanta
humidity_plot <- ggplot(weather_atl, aes(x = time, y = humidity)) +
  geom_line() +
  geom_smooth() +
  labs(x = NULL, y = "Humidity") +
  theme_minimal()
humidity_plot

:::

Right now, these are two separate plots, but we can combine them with + if we load patchwork:

library(patchwork)

temp_plot + humidity_plot

By default, patchwork will put these side-by-side. We can specify that we want the plots to be oriented over/under:

temp_plot / humidity_plot

Or we can change the orientation with the plot_layout() function:

temp_plot + humidity_plot +
  plot_layout(ncol = 1)

We can also play with other arguments in plot_layout(). If we want to make the temperature plot taller and shrink the humidity section, we can specify the proportions for the plot heights. Here, the temperature plot is 70% of the height and the humidity plot is 30%:

temp_plot + humidity_plot +
  plot_layout(ncol = 1, heights = c(0.7, 0.3))

Interesting and excellent real world examples

How to select the appropriate chart type

Many people have created many useful tools for selecting the correct chart type for a given dataset or question. Here are some of the best:

  • The Data Visualisation Catalogue: Descriptions, explanations, examples, and tools for creating 60 different types of visualizations.
  • The Data Viz Project: Descriptions and examples for 150 different types of visualizations. Also allows you to search by data shape and chart function (comparison, correlation, distribution, geographical, part to whole, trend over time, etc.).
  • From Data to Viz: A decision tree for dozens of chart types with links to R and Python code.
  • The Chartmaker Directory: Examples of how to create 51 different types of visualizations in 31 different software packages, including Excel, Tableau, and R.
  • R Graph Catalog: R code for 124 ggplot graphs.
  • Emery’s Essentials: Descriptions and examples of 26 different chart types.

General resources

Visualization in Excel

Visualization in Tableau

Because it is focused entirely on visualization (and because it’s a well-supported commercial product), Tableau has a phenomenal library of tutorials and training videos. There’s a helpful collections of videos here, as well.