Plotting Multiple Graphs in R
Plotting Multiple Graphs in R
By default, R displays one graph at a time. When a new plot command is executed, the previous graph is replaced. R provides several methods to display multiple graphs:
-
Using
par()withmfrow -
Using
par()withmfcol -
Opening multiple graphics windows using
windows() - Saving multiple plots to a PDF file
- Using separate PNG/JPEG files
-
Using
layout() -
Using
split.screen()
1. Using par() Function
The par() function sets graphical parameters.
Syntax
par(mfrow=c(rows, columns))
where
-
rows= number of rows -
columns= number of columns
Example 1: Four Plots in a 2×2 Layout
par(mfrow=c(2,2))
load(iris)
hist(iris$Sepal.Length,
col="skyblue",
main="Histogram")
boxplot(iris$Sepal.Length,
col="green",
main="Box Plot")
plot(iris$Sepal.Length,
iris$Petal.Length,
col="red",
pch=19,
main="Scatter Plot")
barplot(table(iris$Species),
col=rainbow(3),
main="Bar Plot")
Layout:
Restoring Default Setting
par(mfrow=c(1,1))
This restores the default single-graph display.
Example 2: Two Plots Side by Side
par(mfrow=c(1,2))
hist(iris$Sepal.Length)
boxplot(iris$Sepal.Length)
par(mfrow=c(1,1))
Layout:
Example 3: Two Plots Vertically
par(mfrow=c(2,1))
hist(iris$Sepal.Length)
boxplot(iris$Sepal.Length)
par(mfrow=c(1,1))
Layout:
2. Using mfcol
Plots are filled column-wise instead of row-wise.
Example
par(mfcol=c(2,2))
hist(iris$Sepal.Length)
boxplot(iris$Sepal.Length)
plot(iris$Sepal.Length,iris$Petal.Length)
barplot(table(iris$Species))
Arrangement:
1 3
2 4
whereas mfrow gives
3. Using windows()
windows() opens a new graphics window.
Windows only
windows()
hist(iris$Sepal.Length)
Open another window:
windows()
boxplot(iris$Sepal.Length)
Open third window:
windows()
plot(iris$Sepal.Length,
iris$Petal.Length)
Each graph appears in a separate window.
Linux
Use
x11()
instead of windows().
Example:
x11()
hist(iris$Sepal.Length)
Mac OS
Use
quartz()
Example:
quartz()
boxplot(iris$Sepal.Length)
4. Saving Multiple Graphs to a PDF
pdf("graphs.pdf")
hist(iris$Sepal.Length)
boxplot(iris$Sepal.Length)
plot(iris$Sepal.Length,
iris$Petal.Length)
barplot(table(iris$Species))
dev.off()
The file graphs.pdf contains four pages, each with one graph.
5. Saving Individual Graphs as PNG
png("histogram.png")
hist(iris$Sepal.Length)
dev.off()
Another graph:
png("boxplot.png")
boxplot(iris$Sepal.Length)
dev.off()
6. Using layout()
layout() gives more control than par().
Example
layout(matrix(c(1,2,3,4),2,2))
hist(iris$Sepal.Length)
boxplot(iris$Sepal.Length)
plot(iris$Sepal.Length,
iris$Petal.Length)
barplot(table(iris$Species))
Layout:
1 3
2 4
Unequal Layout
layout(matrix(c(1,2,3,3),2,2,byrow=TRUE))
Layout:
1 2
3 3
Plot 3 occupies the entire bottom row.
Example:
7. Using split.screen()
split.screen(c(1,2))
Creates four screens.
Screen 1
screen(1)
hist(iris$Sepal.Length)
Screen 2
screen(2)
boxplot(iris$Sepal.Length)
Checking Active Graphics Devices
Current device:
dev.cur()
Example output:
RStudioGD
2
List active devices:
dev.list()
Example:
2
windows
3
Closing Graphics Device
Close current device:
dev.off()
Close all devices:
graphics.off()
Summary of Methods
| Method | Purpose |
|---|---|
par(mfrow=c(r,c)) | Multiple plots row-wise |
par(mfcol=c(r,c)) | Multiple plots column-wise |
windows() | New graphics window (Windows) |
x11() | New graphics window (Linux) |
quartz() | New graphics window (Mac) |
layout() | Custom plot arrangement |
split.screen() | Divide screen into sections |
pdf() | Save multiple plots to PDF |
png() | Save graph as image |
dev.off() | Close current graphics device |
graphics.off() | Close all graphics devices |
Comments
Post a Comment