Open In App

Draw unbalanced grid of ggplot2 Plots in R

Last Updated : 17 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to Draw an Unbalanced Grid of ggplot2 plots in R Programming Language. We use mainly grid.arrange() and arrangeGrob() functions. Following is the brief information of these two functions. 

  • grid.arrange(): It will calculate a different number of rows and columns to organize the plots and for forming nested layouts of plots

Syntax : grid.arrange(plot, nrow, ncol)

Parameter:

  • plot: ggplot2 plot which we want to arrange
  • nrow: Number of rows
  • ncol: Number of columns

Return: Arrangement of plot

  • arrangeGrob(): It function takes many parameters eg. layout_matrix, as.table, respect, top, bottom, left, right, and many more.

Syntax : arrangeGrob(…, heights)

Parameter:

  • … : ggplot2 plots which we want to arrange on same page
  • heights : Heights of those plots

Return: Arrange Multiple Plots on Same Page with different grid

First, we should load some R packages that are going to be in use further in this example. We will use ggplot2 package for draw plot and gridExtra for drawing various grids on plot.

install.packages("ggplot2")
library("ggplot2")
install.packages("gridExtra")
library("gridExtra")

For the Data of Scatter Plot, we pick some 50 random values for the X and Y axis both using rnorm() function which can generate random normal values. Then create a DataFrame and assign it to the ‘data’ data object.

data <- data.frame(x = rnorm(50), y = rnorm(50))

Now we create a Scatter Plot using ggplot() function and make it Scattered using geom_point() function.

Below is the implementation:

R




# Load Packages
library("ggplot2")
library("gridExtra")
 
# Create data for Plotting
data <- data.frame(x = rnorm(50), y = rnorm(50))
 
# Create Simple Scatter Plot
gplot <- ggplot(data, aes(x, y)) + geom_point(col = "green")
gplot


Output:

Simple Scatter Plot

Simple Scatter Plot

Change Vertical Grid of plot

Here we are changing the vertical grid of the plot. We use grid.arrange() function of gridExtra package. Inside grid.arrange() package, we assign gplot (Our Scatter Plot) as a first parameter and ncol as the second parameter. ncol is just divided the plot area vertical in the value which we assign to ncol. Here we change the plot’s vertical grid to half of the plot space using “ncol = 2”.

Code:

R




# Load Packages
library("ggplot2")
library("gridExtra")
 
# Create Data for plotting
data <- data.frame(x = rnorm(50), y = rnorm(50))
 
# Create ggplot2 Scatter Plot
gplot <- ggplot(data, aes(x, y)) + geom_point(col = "green")
 
# Change the plot grid to half of the plot space.
grid.arrange(gplot, ncol = 2)


Output:

Scatter Plot with half verticle grid

Scatter Plot with half vertical grid

Change Horizontal Grid of Plot

For changing the Horizontal grid of the plot, we simply replace ncol to nrow parameter of grid.arrange() function. nrow parameter takes the value in which we want to divide the plot horizontally. Here we change the plot’s Horizontal grid to half of the plot space using “nrow = 2”.

Code:

R




# Load Packages
library("ggplot2")
library("gridExtra")
 
# Create Data for plotting
data <- data.frame(x = rnorm(50), y = rnorm(50))
 
# Create ggplot2 Scatter Plot
gplot <- ggplot(data, aes(x, y)) + geom_point(col = "green")
 
# Change the plot Horizontal grid to half of the plot space.
grid.arrange(gplot, nrow = 2)


Output:

Scatter Plot with half horizontal grid

Scatter Plot with half horizontal grid

Draw Multiple plots of different grid size on the same Plot Space

For Draw Multiple plots of different sizes, we use arrangeGrob() as the second parameter of grid.arrange() function which will take the height and width of each graph as an argument. First, it will take gplot as an argument for each graph we want to create, and then it also specifies the heights of each plot using the heights parameter. 

Code:

R




# Load Packages
library("ggplot2")
library("gridExtra")
 
# Create Data for plotting
data <- data.frame(x = rnorm(50), y = rnorm(50))
 
# Create ggplot2 Scatter Plot
gplot <- ggplot(data, aes(x, y)) +              
  geom_point(col = "green")
 
# Create Multiple Plots with different size of grids.
grid.arrange(gplot,
             arrangeGrob(gplot, gplot,
                         heights = c(0.4, 0.6)),
             ncol = 2)


Output:

Multiple Plots of different size of grids

Multiple Plots of different size of grids



Similar Reads

Multiple Line Plots or Time Series Plots with ggplot2 in R
In this article, we will discuss how to plot Multiple Line Plots or Time Series Plots with the ggplot2 package in the R Programming Language. We can create a line plot using the geom_line() function of the ggplot2 package. Syntax: ggplot( df, aes( x, y ) ) + geom_line() where, df: determines the dataframe usedx and y: determines the axis variables
2 min read
Draw Multiple ggplot2 plots Side-by-Side
In the R programming language, we can analyze data by creating different graphs or plot out of them. Sometimes for the analysis, we need to see two or more plots at the same time side by side. In that case, we use gridExtra package of R language that divides the frame into grids, and you can add multiple plots in those grids. gridExtra contains a f
2 min read
Combine two ggplot2 plots from different DataFrame in R
In this article, we are going to learn how to Combine two ggplot2 plots from different DataFrame in R Programming Language. Here in this article we are using a scatter plot, but it can be applied to any other plot. Let us first individually draw two ggplot2 Scatter Plots by different DataFrames then we will see how to combine them i.e how draw both
2 min read
How to increase spacing between faceted plots using ggplot2 in R ?
In this article, we will see how to increase spacing between faceted plots using ggplot2 in R Programming language. Note: Here, a line plot has been used the same can be done for any other plot. To create an R plot, we will use ggplot() function and to make a line graph add geom_line() function to it. Finally, for facet grids, we will use facet_gri
3 min read
Data Visualisation using ggplot2(Scatter Plots)
The correlation Scatter Plot is a crucial tool in data visualization and helps to identify the relationship between two continuous variables. In this article, we will discuss how to create a Correlation Scatter Plot using ggplot2 in R. The ggplot2 library is a popular library used for creating beautiful and informative data visualizations in R Prog
7 min read
How To Join Multiple ggplot2 Plots with cowplot?
In this article, we are going to see how to join multiple ggplot2 plots with cowplot. To join multiple ggplot2 plots, we use the plot_grid() function of the cowplot package of R Language. Syntax: plot_grid(plot1,plot2,label=&lt;label-vector&gt;, ncol, nrow) Parameters: plot1 and plot2 are plots that need to be joined.&lt;label-vector&gt; is a vecto
3 min read
How To Make Density Plots with ggplot2 in R?
In this article, we are going to see how to make Density plots with ggtplot2 in R Programming language. The probability density function of a continuous variable is estimated using a data visualization approach called a density plot, sometimes referred to as a kernel density plot or kernel density estimation plot. It offers a rounded picture of the
3 min read
Multiple Density Plots and Coloring by Variable with ggplot2 in R
In this article, we will discuss how to make multiple density plots with coloring by variable in R Programming Language. To make multiple density plots with coloring by variable in R with ggplot2, we first make a data frame with values and categories. Then we draw the ggplot2 density plot using the geom_desnity() function. To color them according t
3 min read
How To Make Violin Plots with ggplot2 in R?
Violin plots help us to visualize numerical variables from one or more categories. They are similar to box plots in the way they show a numerical distribution using five summary-level statistics. But violin plots also have the density information of the numerical variables. It allows visualizing the distribution of several categories by displaying
5 min read
How to Add Vertical Lines By a Variable in Multiple Density Plots with ggplot2 in R
In this article, we will discuss how to add vertical lines by a variable in multiple density plots with ggplot2 package in the R Programming language. To do so first we will create multiple density plots colored by group and then add the line as a separate element. Basic Multiple Density Plot: To make multiple density plots with coloring by variabl
3 min read
Article Tags :