Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 26, 2017 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aravindhebbali/8fc6a6d21c5ab8a0a68f11df51074a9d to your computer and use it in GitHub Desktop.
Save aravindhebbali/8fc6a6d21c5ab8a0a68f11df51074a9d to your computer and use it in GitHub Desktop.
Data Visualization: Combining Plots
# Case Study 1: combine 4 plots in 2 rows and 2 columns
# store the current parameter settings in init
init <- par(no.readonly=TRUE)
# specify that 4 graphs to be combined and filled by rows
par(mfrow = c(2, 2))
# specify the graphs to be combined
plot(mtcars$mpg)
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
# restore the setting stored in init
par(init)
# Case Study 2: combine 2 plots in 1 row and 2 columns
# store the current parameter settings in init
init <- par(no.readonly=TRUE)
# specify that 2 graphs to be combined and filled by rows
par(mfrow = c(1, 2))
# specify the graphs to be combined
hist(mtcars$mpg)
boxplot(mtcars$mpg)
# restore the setting stored in init
par(init)
# Case Study 3: Combine 2 plots in 2 rows and 1 column
# store the current parameter settings in init
init <- par(no.readonly=TRUE)
# specify that 2 graphs to be combined and filled by rows
par(mfrow = c(2, 1))
# specify the graphs to be combined
hist(mtcars$mpg)
boxplot(mtcars$mpg)
# restore the setting stored in init
par(init)
# Case Study 4: Combine 3 plots in 1 row and 3 columns
# store the current parameter settings in init
init <- par(no.readonly=TRUE)
# specify that 3 graphs to be combined and filled by rows
par(mfrow = c(1, 3))
# specify the graphs to be combined
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
# restore the setting stored in init
par(init)
# Case Study 5: Combine 3 plots in 3 rows and 1 column
# store the current parameter settings in init
init <- par(no.readonly=TRUE)
# specify that 3 graphs to be combined and filled by rows
par(mfrow = c(3, 1))
# specify the graphs to be combined
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
# restore the setting stored in init
par(init)
# Case Study 6: Combine 3 plots in 3 rows and 1 column
# store the current parameter settings in init
init <- par(no.readonly=TRUE)
# specify that 4 graphs to be combined and filled by columns
par(mfcol = c(2, 2))
# specify the graphs to be combined
plot(mtcars$mpg)
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
# restore the setting stored in init
par(init)
# layout
# Case Study 1: Combine 4 plots in 2 rows/2 columns filled by rows
# specify the layout
# 4 plots to be combined in 2 row/ 2 columns and arranged by row
layout(matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE))
# specify the 4 plots
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
plot(mtcars$mpg)
# Case Study 2: Combine 4 plots in 2 rows/2 columns filled by columns
# specify the layout
# 4 plots to be combined in 2 row/ 2 columns and filled by columns
layout(matrix(c(1, 2, 3, 4), nrow = 2, byrow = FALSE))
# specify the 4 plots
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
plot(mtcars$mpg)
# Case Study 3: Combine 3 plots in 2 rows/2 columns filled by rows
# specify the matrix
matrix(c(1, 1, 2, 3), nrow = 2, byrow = TRUE)
# 3 plots to be combined in 2 row/ 2 columns and arranged by row
layout(matrix(c(1, 1, 2, 3), nrow = 2, byrow = TRUE))
# specify the 3 plots
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
# Case Study 4: Combine 3 plots in 2 rows/2 columns filled by rows
# specify the matrix
matrix(c(1, 2, 3, 3), nrow = 2, byrow = TRUE)
# 3 plots to be combined in 2 row/ 2 columns and arranged by row
layout(matrix(c(1, 2, 3, 3), nrow = 2, byrow = TRUE))
# specify the 3 plots
hist(mtcars$mpg)
boxplot(mtcars$mpg)
plot(mtcars$disp, mtcars$mpg)
# Case Study 5: Combine 3 plots in 2 rows/2 columns filled by columns
# specify the matrix
matrix(c(1, 1, 2, 3), nrow = 2, byrow = FALSE)
# 3 plots to be combined in 2 row/ 2 columns and arranged by columns
layout(matrix(c(1, 1, 2, 3), nrow = 2, byrow = FALSE))
# specify the 3 plots
hist(mtcars$mpg)
plot(mtcars$disp, mtcars$mpg)
boxplot(mtcars$mpg)
# Case Study 6: Combine 3 plots in 2 rows/2 columns filled by columns
# specify the matrix
matrix(c(1, 2, 3, 3), nrow = 2, byrow = FALSE)
# 3 plots to be combined in 2 row/ 2 columns and arranged by columns
layout(matrix(c(1, 2, 3, 3), nrow = 2, byrow = FALSE))
# specify the 3 plots
boxplot(mtcars$mpg)
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
# Case Study 7: Width of the 2nd column is twice the width of the 1st column
# specify the matrix
matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE)
# 4 plots to be combined in 2 row/ 2 columns and arranged by columns
layout(matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE), widths = c(1, 3))
# specify the plots
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
plot(mtcars$mpg)
# Case Study 8: Width of the 2nd column is twice that of the first and last column
# specify the matrix
matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE)
# 6 plots to be combined in 2 row/ 3 columns and filled by rows
layout(matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE), widths = c(1, 2, 1))
# specify the plots
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
plot(mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
# Case Study 9: Height of the 2nd row is twice that of the first row
# 4 plots to be combined in 2 row/ 2 columns and filled by rows
layout(matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE), heights= c(1, 2))
# specify the 4 plots
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
plot(mtcars$mpg)
# Putting it all together: Before we end this section, let us combine plots using both the widths and heights option
# specify the matrix
matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, byrow = TRUE)
# 6 plots to be combined in 3 row/ 2 columns and arranged by rows
layout(matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, byrow = TRUE), heights= c(1, 2, 1),
widths = c(2, 1))
# specify the 6 plots
plot(mtcars$disp, mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
plot(mtcars$mpg)
hist(mtcars$mpg)
boxplot(mtcars$mpg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment