Plotting Multiple Plots Together in R: A Guide to Facets and Themes

R Code for Plotting Multiple Plots Together

=====================================================

In this article, we will explore ways to plot multiple bar and scatter plots tightly together in R. We’ll use the ggplot2 library for creating these plots, and discuss how to align them without gaps.

Introduction


When working with multiple datasets, it’s often useful to visualize all the data points at once. However, when dealing with large datasets or complex visualizations, the resulting plots can become cluttered and hard to read.

To address this issue, we’ll explore two main approaches:

  1. Faceting: This involves dividing each dataset into separate panels and displaying them side-by-side.
  2. Combining figures using themes and packages: We’ll discuss how to create a unified look for multiple plots by applying the same theme across all of them.

Faceting Approach


One efficient way to plot multiple, aligned plots is by using facets from ggplot2. In this approach, we define separate data frames for each dataset and then use facets to display them side-by-side.

Here’s an example code snippet that demonstrates how to achieve this:

# Load the necessary libraries
library(ggplot2)

# Create sample datasets
river1 = data.frame(year     =  1991:2017, 
                    flow     =  rnorm(n=27, mean = 900, sd = 350),
                    salinity =  rnorm(n=27, mean = 3000, sd = 800))
river2 = data.frame(year     =  1992:2019, 
                    flow     =  rnorm(n=28, mean = 20000, sd = 2400),
                    salinity =  rnorm(n=28, mean = 700, sd = 1500))

# Combine the datasets into a single data frame
rivers <- bind_rows(river1 = river1, river2 = river2, .id = "River") %>% 
  pivot_longer(cols = c("flow", "salinity"), names_to = "variable", values_to = "value")

# Create the plots using facets
ggplot(data = filter(rivers, variable == "salinity" ), mapping = aes(x = year, y = value)) +
  geom_col(data = filter(rivers, variable == "flow" ), fill = "blue") +
  geom_point(color="darkred") +
  geom_line(color="darkred") +
  facet_grid(River ~ ., scales = "free_y")

Combining Figures Using Themes


Another approach is to create multiple plots individually and then combine them using a unified theme. This method allows for more flexibility in terms of plot design, but may require more work when creating the overall visualization.

Here’s an example code snippet that demonstrates how to achieve this:

# Load the necessary libraries
library(ggpubr)
library(patchwork)

# Create sample datasets
river1 = data.frame(year     =  1991:2017, 
                    flow     =  rnorm(n=27, mean = 900, sd = 350),
                    salinity =  rnorm(n=27, mean = 3000, sd = 800))
river2 = data.frame(year     =  1992:2019, 
                    flow     =  rnorm(n=28, mean = 20000, sd = 2400),
                    salinity =  rnorm(n=28, mean = 700, sd = 1500))

# Create the plots individually
(r1sal = ggplot(river1, aes(x = year, y = salinity)) + 
    geom_point(stat = "identity", color="darkred", fill="darkred", size = 2) + 
    labs(title = "River 1 flow and flow-weighted salinity", x = "", y = "Streamflow (ML)") +
    scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

(r1flow = ggplot(river1, aes(x = year, y = flow)) + 
    geom_bar(stat = "identity", color="blue", fill="lightblue") + 
    labs(x = "Water year", y = "Streamflow (ML)") +
    scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

(r2sal = ggplot(river2, aes(x = year, y = salinity)) + 
    geom_point(stat = "identity", color="darkred", fill="darkred", size = 2) + 
    labs(title = "River 2 flow and flow-weighted salinity", x = "Water year", y = "Streamflow (ML)") +
    scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

(r2flow = ggplot(river2, aes(x=year, y=flow)) + 
       geom_bar(stat = "identity", color="blue", fill="lightblue") + 
       labs( x = "Water year", y = "Streamflow (ML)") +
       scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

# Combine the plots using patchwork
ggarrange(r1sal, r1flow, r2sal, r2flow,
            labels = c("A", "B", "C","D"),
            ncol = 1, nrow = 4) +
  theme(axis.text.x = element_blank()) +
  theme(axis.ticks.x = element_blank())

Best Practices


When creating multiple plots, consider the following best practices:

  • Align axes: Make sure to align the x-axes of all plots so they appear contiguous.
  • Use consistent themes: Choose a unified look for your plot by applying the same theme across all plots. This will help create a cohesive visual presentation.
  • Remove unnecessary elements: Remove any unnecessary elements, such as tick marks or labels, to improve readability.

Conclusion


Plotting multiple plots tightly together can be achieved using either facets or combining figures with unified themes. Both approaches have their own advantages and disadvantages, so choose the one that best fits your needs.

By following these guidelines and best practices, you’ll be able to create informative and visually appealing plots for your data analysis.


Last modified on 2024-10-06