Arranging with different widths
The second combination of these plots would be arranging them in different widths in multiple rows and columns in the same graph window. The different width orientation arranges the plots in different lines according to widths. Basically, the first plot is placed on the first line and the other two plots are placed on the next line horizontally.
We use a slash(/) operator at that position where we want to start a new line. So we placed it after the first plot. Then, we use add(+) operator to arrange the other two plots horizontally on the next new line.
Example: Arranging plots with different widths
R
library (ggplot2) library (patchwork) data (iris) head (iris) gfg1 <- ggplot (iris, aes (Sepal.Length, Petal.Length, color = Species)) + geom_bar (stat = "identity" ) gfg2 <- ggplot (iris, aes (Sepal.Length, Petal.Length, color = Species)) + geom_point () gfg3 <- ggplot (iris, aes (Sepal.Length, Petal.Length, color = Species)) + geom_line () # Arranging the plots in different widths gfg_comb_2 <- gfg1 / (gfg2 + gfg3) gfg_comb_2 |
Output :
Draw Composition of Plots Using the patchwork Package in R
In this article, we will discuss how to draw a composition of plots using the patchwork package in R programming language. The patchwork package makes it easier to plot different ggplots in a single graph. We will require both ggplot2 packages for plotting our data and the patchwork package to combine the different ggplots.
Let us first draw all the plots normally and independently.
Example: Plotting the dataset in a bar plot
R
library (ggplot2) library (patchwork) data (iris) head (iris) # Plotting the bar chart gfg1 <- ggplot (iris, aes (Sepal.Length, Petal.Length, color = Species)) + geom_bar (stat = "identity" ) gfg1 |
Output :
Example: Plotting the dataset in a scatterplot
R
library (ggplot2) library (patchwork) data (iris) head (iris) # Scatterplot gfg2 <- ggplot (iris, aes (Sepal.Length, Petal.Length, color = Species)) + geom_point () gfg2 |
Output :
Example: Plotting the dataset in a line plot
R
library (ggplot2) library (patchwork) data (iris) head (iris) # Line plot gfg3 <- ggplot (iris, aes (Sepal.Length, Petal.Length, color = Species)) + geom_line () gfg3 |
Output :
Now let us look at how these three plots can be combined in various orientations.