In R Programming Language, you can combine plots using ‘par’ function. Combining plots will help you to make decisions easily. Comparing results will be easy by combining plots. par() function is used to set the parameters for multiple plots, and the layout() function determines how the plots should be arranged. To combine different plots first individual plots should be created, you can use the plot() and legend() functions to add every single plot to a single plotting window.
Why do you have to combine plots?
While performing data analysis there may be a situation where we need to compare both plots and make decisions according to them. Data analysts combine plots to look at different plots at the same time.
Parameters in par() function
Parameter |
Description |
mfrow() | used to specify the plots row-wise |
mfcol() | used to specify the plots column wise |
layout() | It takes a matrix as its argument, where each element of the matrix represents a plotting region in the layout |
Using mfrow to combine plots
We will use the palmerpenguins data set for a while. In this example, we create two single plots and combine them row-wise using the par() function and mfrow parameter.
#install palmerpenguins package install.packages ( "palmerpenguins" )
library (palmerpenguins)
# combined plot with 2 rows and 1 column par (mfrows = c (2,1))
# first single plot plot (penguins$body_mass_g,
penguins$flipper_length_mm,
main = "ScatterPlot" ,
xlab = "body_mass" ,
ylab = "flipper_length" )
# second single plot plot (penguins$bill_depth_mm,
penguins$bill_length_mm,
main = "ScatterPlot" ,
xlab = "bill_depth" ,
ylab = "bill_length" )
|
Output:
Using mfcol to combine plots
In this example, we create two single plots and combine them column-wise using par() method and mfcol parameter.
#create data math_marks <- c (90, 95, 85, 76, 65)
science_marks <- c (45, 98, 54, 87, 65)
student_names <- c ( "A" , "B" , "C" , "D" , "E" )
# combine plots with 1 row and 2 columns par (mfcol = c (1,2))
# first pie chart pie (math_marks, student_names,
main = "PieChart" , radius = 0.75)
# second pie chart pie (science_marks, student_names,
main = "Piechart" , radius = 0.75)
|
Output:
Using layout() to combine plots
We use the layout() parameter to combine plots according to our requirements and we use layout() to customize the combined plot.
Note: layout() function can be used only within an open device, otherwise you will get an error.
To avoid errors while using layout() function use the windows() function which creates a new window to combine plots.layout() takes a matrix as its arguments along with nrow and ncol etc.
game <- data.frame (scores <- c (34, 54, 21, 67, 98),
players <- c ( "A" , "B" , "C" , "D" , "E" ),
avg_score <- c (56, 43, 65, 23, 16))
# display plot on screen windows ()
# create layout layout ( matrix ( c (1, 2, 3, 4), nrow = 2,
ncol = 2, byrow = TRUE ))
# first plot plot (game$scores, game$avg_score,
main = "ScatterPlot" ,
xlab = "Score_in_game" ,
ylab = "avg_score" )
# second plot boxplot (game$avg_score, main = "Boxplot" )
# third plot hist (game$scores, main = "Histogram" ,
xlab = "scores" )
# fourth plot pie (game$avg_score, game$players,
main = "Piechart" , radius = 0.75)
|
Output:
To display the layout of the plot, you can use layout.show() function. More examples of layout() function
par (mfrow = c (2, 2), mar= c (5, 5, 5, 5))
layout ( matrix ( c (2, 0, 1, 3), 2, 2, byrow = TRUE ),
widths= c (3, 3), heights= c (1, 3))
# Create sample data set.seed (123)
x <- rnorm (100)
y <- rnorm (100)
# Create barplot barplot (x, main = "Bar Plot" , col = "blue" )
# Create scatterplot plot (x, y, main = "Scatter Plot" ,
col = "red" , pch = 16)
# Create densityplot d <- density (x)
plot (d, main = "Density Plot" ,
col = "green" , type = "l" )
|
Output:
layout ( matrix ( c (1,2,3,3), ncol=2, byrow= TRUE ))
x <- rnorm (100)
y <- rnorm (100)
plot ( density (x), col = "green" )
plot (x, y, 'p' , col = "red" )
barplot (x, col = "yellow" )
|
Output:
We can create just a simple layout without any graphs in it for demonstration purposes.
example <- layout ( matrix ( c (2, 0, 1, 3),
2, 2, byrow = TRUE ))
# display layout layout.show (example)
|
Output: