# How to Plot Multiple Plots on Same Graph in R

Visual representation of data plays a crucial role in the field of data science and analytics. It simplifies complex data and helps in understanding the underlying patterns and trends. R, an open-source language and environment for statistical computing, provides robust support for creating high-quality plots. In particular, R allows users to overlay multiple plots on the same graph. This ability is valuable when comparing multiple datasets or viewing different aspects of the same data.

In this article, we will delve into various methods to plot multiple plots on the same graph in R. We’ll start with the basic techniques using base R, then explore packages like ggplot2 and lattice, which offer more sophisticated ways to create multi-plot graphs.

## 1. Plotting Multiple Plots Using Base R

In base R, the plot() function is the primary tool for creating plots. To add more plots to an existing plot, you can use the lines(), points(), abline(), polygon(), and text() functions.

### 1.1 Using the lines() function

After creating an initial plot with plot(), you can add more lines with the lines() function:

# Create first plot
x <- 1:10
y1 <- x ^ 2
plot(x, y1, type = "l", col = "red", xlab = "X-axis", ylab = "Y-axis", main = "Multiple Plots")

y2 <- x ^ 1.5
lines(x, y2, type = "l", col = "blue")

In this example, type = "l" specifies that the plot should be a line plot. The col argument determines the color of the line.

### 1.2 Using the points() function

The points() function can be used to add more points to an existing scatter plot:

# Create first scatter plot
x <- 1:10
y1 <- x ^ 2
plot(x, y1, xlab = "X-axis", ylab = "Y-axis", main = "Multiple Plots")

y2 <- x ^ 1.5
points(x, y2, col = "blue", pch = 20)

The pch argument in the points() function determines the shape of the points.

## 2. Overlaying Plots Using the par() Function in Base R

The par() function can be used to set or query graphical parameters. One of these parameters is new, which can be set to TRUE to allow for new plots to be overlaid on top of existing ones.

# Create the first plot
plot(1:10, (1:10) ^ 2, type = "l", col = "red", xlab = "", ylab = "", main = "Overlaying Plots")
par(new = TRUE)

# Overlay the second plot
plot(1:10, (1:10) ^ 1.5, type = "l", col = "blue", xlab = "X-axis", ylab = "Y-axis")

In this example, the first plot() call creates a plot, and par(new = TRUE) allows a second plot to be added to the same graphic. Note that we left xlab and ylab blank in the first plot and specified them only in the second to prevent label overlapping.

## 3. Plotting Multiple Plots Using the matplot() Function

The matplot() function can be used to create a multi-line plot more simply:

x <- 1:10
y <- matrix(1:20, nrow = 10)

matplot(x, y, type = c("p", "l"), col = c("red", "blue"), xlab = "X-axis", ylab = "Y-axis", main = "Multiple Plots with matplot()")

In this example, the type argument specifies both points and lines to be plotted. y is a matrix, each of whose columns corresponds to a line on the plot.

## 4. Using ggplot2 for Multiple Plots

ggplot2 is a powerful R package based on the principles of “The Grammar of Graphics” that allows you to create complex multi-layered graphics. It’s part of the tidyverse suite of packages.

### 4.1 Overlaying Plots Using ggplot2

To overlay plots in ggplot2, you can add layers to your graph using + operator:

library(ggplot2)

df <- data.frame(x = 1:10, y1 = (1:10) ^ 2, y2 = (1:10) ^ 1.5)

ggplot(df, aes(x)) +
geom_line(aes(y = y1), color = "red") +
geom_line(aes(y = y2), color = "blue")

This code first creates a data frame df with three columns: x, y1, and y2. The ggplot(df, aes(x)) function initializes the plot using df and sets x as the common x-axis. The geom_line() functions then add layers to the plot for y1 and y2.

### 4.2 Using Facets in ggplot2

Facets in ggplot2 allow you to create subplots that each display one subset of the data:

library(ggplot2)

# Reshape the data
df <- data.frame(x = rep(1:10, 2), y = c((1:10) ^ 2, (1:10) ^ 1.5), type = rep(c("y1", "y2"), each = 10))

ggplot(df, aes(x, y, color = type)) +
geom_line() +
facet_wrap(~ type)

Here, facet_wrap(~ type) creates separate subplots for y1 and y2. Each subplot uses the same x-axis and y-axis.

## 5. Using lattice for Multiple Plots

lattice is another powerful R package for creating complex multi-layered graphics. It’s inspired by Trellis graphics, a framework for data visualization.To create multiple plots with lattice, you can use the xyplot() function:

install.packages('lattice')

library(lattice)

df <- data.frame(x = rep(1:10, 2), y = c((1:10) ^ 2, (1:10) ^ 1.5), type = rep(c("y1", "y2"), each = 10))

xyplot(y ~ x | type, data = df, type = "l", auto.key = list(points = FALSE, lines = TRUE))

In this example, y ~ x | type indicates that y should be plotted against x, with different plots created for each value of type. The auto.key argument is used to add a legend to the plot.

In conclusion, R offers several methods for plotting multiple plots on the same graph. The choice of method depends on your specific needs and the complexity of the data. The flexibility and versatility of R’s plotting system make it a powerful tool for data visualization.

Posted in RTagged