library(ggplot2)
library(dplyr)
library(haven)
library(here)
library(grid)
library(gridExtra)
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:
Let’s create some sample charts for this chapter for a few countries, starting in 1980:
library(dplyr)
library(ggplot2)
<- read_dta(here("databases/WEOApr2023Pub.dta"))
weo <- c(111, 112, 213, 911, 138, 534)
country_codes
<- list()
plot_list
for (code in country_codes) {
# Filter for the specific country and years >= 1980
<- weo %>%
country_data filter(ifscode == code, year >= 1980)
# Extract country name
<- unique(country_data$country) # Use unique to avoid issues
country_name
# Create the plot
<- ggplot(country_data, aes(x = year, y = ngdp_r_ppp)) +
plot geom_line(color = "blue", size = 1) +
labs(x = "Year", y = "Real GDP in PPP terms", title = paste(country_name))
# Save plot in the list
as.character(code)]] <- plot
plot_list[[
}
# Assign and print each plot
for (i in 1:length(country_codes)) {
<- paste0("plot", i)
plot_name 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()
:
<- grid.arrange(plot1, plot2, ncol = 2) combined_panel
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:
<- plot1 / plot2
combined_plot
plot(combined_plot)
If you want to combine the plots horizontally, simply replace the /
operator with the |
or the +
operator. For example:
<- plot1 | plot2
combined_plot_2
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 | plot2)/
(plot1 | plot4)/
(plot3 | plot6)
(plot5
<- combined_plot_3 + plot_annotation(
combined_plot_3_output 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)