Combining charts into panels

In R, you can combine multiple charts into chart panels using various packages and functions. Here we present two of them.

Using grid.arrange()

One of the most commonly used packages for creating chart panels is again ggplot2 and the gridExtra package for arranging the panels. Let’s assume you have multiple ggplot2 plots that you want to combine into a panel. You can use the grid.arrange() function from the gridExtra package to arrange the plots in a grid or any other custom layout.

First, install and load the necessary packages:

library(ggplot2)
library(dplyr)
library(haven)
library(here)
library(grid)
library(gridExtra)

Let’s create some sample charts for this chapter for a few countries, starting in 1980:

library(dplyr)
library(ggplot2)

weo <- read_dta(here("databases/WEOApr2023Pub.dta"))
country_codes <- c(111, 112, 213, 911, 138, 534)

plot_list <- list()

for (code in country_codes) {
  # Filter for the specific country and years >= 1980
  country_data <- weo %>% 
    filter(ifscode == code, year >= 1980)
  
  # Extract country name
  country_name <- unique(country_data$country)  # Use unique to avoid issues
  
  # Create the plot
  plot <- ggplot(country_data, aes(x = year, y = ngdp_r_ppp)) +
    geom_line(color = "blue", size = 1) +
    labs(x = "Year", y = "Real GDP in PPP terms", title = paste(country_name))
  
  # Save plot in the list
  plot_list[[as.character(code)]] <- plot
}

# Assign and print each plot
for (i in 1:length(country_codes)) {
  plot_name <- paste0("plot", i)
  assign(plot_name, plot_list[[as.character(country_codes[i])]])
  print(plot_list[[as.character(country_codes[i])]])
}

Combine the plots into a chart panel using grid.arrange():

combined_panel <- grid.arrange(plot1, plot2, ncol = 2)

plot(combined_panel)

This will arrange plot1 and plot2 into a 2-column panel. You can adjust the ncol argument to control the number of columns in the panel. You can also use ncol and nrow to specify the number of rows and columns if you want a more customized layout. You can also combine more than two plots into a single panel by adding more plots as arguments to grid.arrange().

Using patchwork

Optionally, you may use the patchwork package to combine charts into panels. patchwork is an R package that provides a flexible and intuitive way to combine and arrange multiple ggplot2 plots into a single composite panel.

First use the code below to install and load the package:

library(patchwork)
# if it has not been installed, use the following code:
# install.packages("devtools")
# devtools::install_github("thomasp85/patchwork")

Here’s a simple example of how to use patchwork to combine two ggplot2 plots vertically:

combined_plot <- plot1 / plot2

plot(combined_plot)

If you want to combine the plots horizontally, simply replace the / operator with the | or the + operator. For example:

combined_plot_2 <- plot1 | plot2

plot(combined_plot_2)

You may also play with different operators to create a six-plot panel with title, subtitle, and footnotes:

combined_plot_3 <- 
  (plot1 | plot2)/
  (plot3 | plot4)/
  (plot5 | plot6)

combined_plot_3_output <- combined_plot_3 + plot_annotation(
  title = 'GDP trend chart panel example', 
  subtitle = '(In billions of US dollars)',
  caption="Sources: WEO and IMF staff calculations.\nNote: This panel uses data from WEO Apr 2023 vintage.",
  theme = theme(plot.title = element_text(color="black",face="bold", size=24),
                plot.caption = element_text(hjust = 0,vjust = 5.5, size=12)
  ))

plot(combined_plot_3_output)