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
1.1 Using the lines() function
After creating an initial plot with
plot(), you can add more lines with the
# 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") # Add a second plot 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
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") # Add more points y2 <- x ^ 1.5 points(x, y2, col = "blue", pch = 20)
pch argument in the
points() function determines the shape of the points.
2. Overlaying Plots Using the par() Function in Base R
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
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
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
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:
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
4.2 Using Facets in ggplot2
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)
facet_wrap(~ type) creates separate subplots for
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
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
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.