Mixing Plots with the ggpubr Package
Learn how to use the ggpubr package and its ggarrange function to create customized plots in ggplot2.
The ggplot
extension packages in R allow using multiple plots, offering a powerful data visualization approach. These packages facilitate data scientists and analysts to create rich and informative visualizations for their projects. Various ggplot
extension packages help combine multiple ggplot
plots for effective data analysis. Similar to the patchwork
and cowplot
packages, there is another package, the ggpubr
package.
Let’s explore how to create beautiful data visualizations containing multiple plots using ggpubr
.
Getting started with ggpubr
The ggpubr
package is a widely used package, particularly among those who use ggplot2
for their plotting needs. The package’s most notable function is ggarrange()
.
The ggarrange()
function allows us to easily arrange multiple plots in a single figure while providing further customization options.
Additionally, ggpubr
offers a variety of other functions for adding mean lines and
First, let’s load the ggpubr
package using the following code:
library (ggpubr)
Plotting with the ggarrange()
function
The ggarrange()
function in the ggpubr
package allows us to control different parts of a plot, like the individual plots, labels, and adding a common legend. Other packages like cowplot
and patchwork
don’t offer this level of control.
We’ll set theme_bw()
as the default theme for all the plots. We’ll also fix the position of the legend to the bottom, rotate the x-axis labels, and define the color palette to eliminate the need to change the overall appearance of each graph separately using the following code:
theme_set(theme_bw() +theme(legend.position = "bottom")+theme(axis.text.x = element_text(angle = 90)))colors <- c("#0b7a75", "#1DD3B0", "#5DA9E9", "#00487C", "#03045E")
- Lines 1-3: We use the
theme_set()
function to change the default gray theme totheme_bw()
. We also set thelegend.position
argument tobottom
inside thetheme()
function, and we specify theangle
argument inside theelement_text()
function to rotate the x-axis labels by90
degrees. - Line 4: We create a vector called
colors
using thec()
function with multiple elements where each element represents a hex color code.
Now, we’ll create four ...