Multiple Plots with the Cowplot Package
Learn how to combine multiple plots using cowplot for data visualization.
Introduction to the cowplot package
Presenting information through multiple plots in a data visualization can provide a comprehensive and holistic view of complex data. Using multiple plots can help us quickly identify outliers and assist in an adequate representation of the underlying information.
In this context, let’s explore a powerful package called cowplot, which allows us to combine multiple ggplot2 plots into a single plot.
The cowplot package is a popular add-on package for ggplot2. It allows for easy alignment and spacing of multiple plots. Furthermore, it provides various options for customizing the appearance of the combined plot.
When compared to the patchwork package, managing the plot spacing and alignment becomes easier in the cowplot package, especially when working with different aspect ratios. More information about the cowplot package can be found here.
First, let’s load the cowplot package using the following command:
library(cowplot)
Next, we’ll set theme_bw() as the default theme for all the plots. We’ll define the color palette and use the theme() function to set the legend position to the bottom of the graph for all of our graphs created using the cowplot package.
colors <- c("#0b7a75","#1DD3B0","#5DA9E9","#00487C","#03045E")theme_set(theme_bw() +theme(legend.position = "bottom",plot.title = element_text(hjust = 0.5)))
- Line 1: We use the
c()function for creating a vector calledcolorand store multiple colors or elements, each representing a hex color code. - Lines 2–3: We set the default theme to
theme_bw(). We also change the default position of the legend by setting thelegend.positionargument tobottominside thetheme()function. - Line 4: We center align the plot title by setting the
hjustargument as0.5.
Plotting with cowplot
We’ll create four separate charts and store them in the chart1, chart2, chart3, and chart4 objects using the following code. This will make it easier for us to access and reproduce the plots as needed. We’ll also add a title to each plot for reference using the labs() function.
# Line chartdf <- gapminder %>% filter(country %in% c("United States", "Canada","Mexico"))chart1 <- ggplot(df) +geom_line(aes(x = year, y = gdpPercap, color = country),linewidth = 1)+scale_color_manual(values = colors) + labs(title ="Line chart")# Histogramchart2 <- ggplot(diamonds) +geom_histogram(aes(x = price),color = "#5da9e9", fill ="#03045e", bins = 30) + labs(title ="Histogram")# Boxplotchart3 <- ggplot(housing) +geom_boxplot(aes(x = Type, y = Freq, fill = Infl)) +scale_fill_manual(values = colors) + labs(title ="Box plot")# Violin plotchart4 <- ggplot(warpbreaks, aes(x = tension, y = breaks, fill = wool)) +geom_violin(position = position_dodge(0.8), trim = FALSE) +geom_boxplot(position = position_dodge(0.8), color = "white", width = 0.1)+scale_fill_manual(values=colors) + labs(title ="Violin plot")
- Line 2: We create a new
dfdata frame for storing the filtered data of thegapminderdataset using thefilter()function. The newdfdata frame will include only the rows where the country isUnited States,Canada, and