Creating High-Quality Plots with Base R: A Guide to Multiplots

Base R Plots with Shared Title and X-Axis Label

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

In this tutorial, we will explore how to create two base R plots side by side, sharing the same title and x-axis label. We will delve into the layout() function, which allows us to arrange multiple plots in a single figure.

Introduction


Base R provides an efficient way to create high-quality plots using its built-in graphics engine. One of the common use cases is creating multiple plots side by side or above/below each other. The layout() function is particularly useful for this purpose, as it allows us to arrange multiple plots in a single figure.

Understanding the Layout Function


The layout() function takes several arguments that control how the plots are arranged:

  • nrow: This argument specifies the number of rows.
  • ncol: This argument specifies the number of columns.
  • outer = TRUE (optional): When set to TRUE, this argument places the x-axis labels on the outside of the plot.

Here’s a basic example of how to use the layout() function:

# Create two plots side by side using layout()
layout(matrix(c(1, 2), nrow = 1))

# Create the first plot
plot(1:10, 1:10, main = "Plot 1", type = "l", col = "blue", xlab = "")

# Create the second plot without the x-axis
plot(1:10, (1:10)^2, main = "", type = "l", col = "red")

However, this approach has some limitations. For example, if the plots have different axis limits, we cannot simply adjust the nrow and ncol arguments to make it work.

A Better Approach: Using mtext()


Instead of using the layout() function directly, we can use the mtext() function to add x-axis labels. The key argument is outer = TRUE, which places the label on the outside of the plot.

Here’s a modified version of the previous code:

# Create two plots side by side using layout()
layout(matrix(c(1, 2), nrow = 1))

# Create the first plot
plot(1:10, 1:10, main = "Plot 1", type = "l", col = "blue", xlab = "")

# Create the second plot without the x-axis
plot(1:10, (1:10)^2, main = "", type = "l", col = "red")

# Add labels to the x-axis if needed
mtext("Common X-axis Label", side = 1, line = -2, outer = TRUE)

# Add title to both plots
mtext("My Multiplot Title", side = 3, line = -2, outer = TRUE)

In this code snippet, we use mtext() with the outer argument set to TRUE. This allows us to place the x-axis labels outside of the plots and create a clean appearance.

Combining Multiple Plots


Now that we have mastered using the layout() function and mtext(), let’s combine multiple plots into a single figure. Here’s an example:

# Create two rows with one column each
layout(matrix(c(1, 2), nrow = 2))

# Create the first plot
plot(1:10, 1:10, main = "Plot 1", type = "l", col = "blue", xlab = "")

# Create the second plot without the x-axis
plot(1:10, (1:10)^2, main = "", type = "l", col = "red")

# Add labels to the x-axis if needed
mtext("X-axis Label 1", side = 3, line = -2, outer = TRUE)

# Create a third plot
plot(11:20, 21:30, main = "Plot 3", type = "l", col = "green")

# Add labels to the x-axis if needed
mtext("X-axis Label 2", side = 1, line = -2, outer = TRUE)

In this code snippet, we create a two-row layout using matrix(). We then add multiple plots to each row and use mtext() with outer set to TRUE to position the x-axis labels outside of the plots.

Conclusion


Creating high-quality plots in base R can seem daunting at first, but mastering the layout() function and mtext() allows us to create clean and professional-looking plots. By following this tutorial, you should now have a solid understanding of how to create multiple plots side by side or above/below each other using base R.

Tips for Improving Your Plotting Skills


  • Use mtext() for labels: Instead of relying solely on the xlab argument, use mtext() with outer = TRUE to position your labels outside of the plots.
  • Experiment with different layouts: Try using matrix() or other layout functions to find the arrangement that works best for your plot.
  • Pay attention to axis limits: Make sure that the axis limits are consistent across all plots in your figure.

By following these tips and mastering the layout() function, you’ll be well on your way to creating high-quality base R plots.


Last modified on 2025-05-06