Plotting Multiple Graphs on a Single Plot in R

Introduction

Visualizing data is an essential part of any data analysis process. In many scenarios, it becomes necessary to compare multiple datasets or trends within the same graph for better insights and easier comparison. This tutorial will guide you through different methods available in R to plot multiple graphs on a single plot.

Plotting with Base Graphics

Using lines() Function

In base R graphics, after creating an initial plot using the plot() function, additional plots can be added by utilizing the lines() or points() functions. This method effectively overlays new data onto an existing graph without opening a new plotting window.

Example:

# Generating sample data
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)         # Normal distribution (mean=0, sd=1)
y2 <- pnorm(x, 1, 1)   # Normal distribution with mean=1, sd=1

# Plot the first dataset
plot(x, y1, type = "l", col = "red", main = "Two Probability Density Functions",
     xlab = "x", ylab = "Probability Density")

# Add the second dataset to the existing plot
lines(x, y2, col = "green")

Using par(new=TRUE)

The par() function can be used to overlay plots on different axes but within the same plotting window. This method is useful for comparing datasets with different scales or units.

Example:

# Plot the first dataset
plot(x, y1, type = "l", col = "red", main = "Overlay Plot with Different Axes",
     xlab = "x", ylab = "y1")

# Enable overlay plotting on a new set of axes
par(new = TRUE)

# Plot the second dataset using new axes
plot(x, y2, type = "l", col = "green", axes = FALSE, xlab = "", ylab = "")

Using matplot() Function

The matplot() function is particularly useful when plotting multiple columns of data from a matrix or dataframe against the same set of x-values.

Example:

# Prepare data
y_data <- cbind(y1, y2)

# Plot using matplot
matplot(x, y_data, type = "l", col = c("red", "green"),
        main = "Using matplot for Multiple Lines",
        xlab = "x", ylab = "Probability Density")

legend("topright", legend = c("y1", "y2"), fill = c("red", "green"))

Using ggplot2 Package

The ggplot2 package offers a powerful and flexible system for creating graphics. It is built on the concept of layered plots, where you can build up a plot incrementally.

Example:

# Load ggplot2 library
library(ggplot2)

# Create data frame
df <- data.frame(x = x, y1 = y1, y2 = y2)

# Plot using ggplot2
ggplot(df, aes(x)) +
  geom_line(aes(y = y1), colour = "red") + 
  geom_line(aes(y = y2), colour = "green") +
  labs(title = "Multiple Lines with ggplot2", x = "x", y = "Probability Density")

Conclusion

In this tutorial, we explored several methods to plot multiple graphs on a single plot in R. Each approach has its unique advantages and use cases: base graphics are straightforward for simple overlays; matplot() is great when dealing with matrix-like data structures; and ggplot2 provides extensive customization options and an aesthetically pleasing output. Choose the method that best fits your specific needs.

Best Practices

  • Always ensure your x-values (and y-values if necessary) are consistent across plots for accurate comparisons.
  • Use different colors or line types to distinguish between datasets.
  • Include a legend when plotting multiple series on the same graph.
  • Leverage ggplot2’s extensive capabilities to create publication-quality graphics.

Leave a Reply

Your email address will not be published. Required fields are marked *