Open In App

R ggplot2 – Marginal Plots

A marginal plot is a scatterplot that has histograms, boxplots, or dot plots in the margins of the x- and y-axes. It allows studying the relationship between 2 numeric variables. The base plot visualizes the correlation between the x and y axes variables. It is usually a scatterplot or a density plot. The marginal charts are commonly plotted on the top and right margin of the base plot and they show the distribution of x and y axes variables using a histogram, barplot, or density plot. This helps us to visualize the distribution intensity at different values of variables along both axes. 

To plot a marginal plot in the R  Language, we will use the ggExtra package of the R Language. The ggExtra is a collection of functions and layers to enhance ggplot2. The ggMarginal() function can be used to add marginal histograms/boxplots/density plots to ggplot2 scatterplots.



Installation:

To install the ggExtra package we use:

install.packages("ggExtra")

Creation of Marginal plots

To create marginal plots we use the following function to make a marginal histogram with a scatter plot.



Syntax: ggMarginal( plot, type )

Parameters:

  • plot: Determines the base scatter plot over which marginal plot has to be added.
  • type: Determines the type of marginal plot i.e. histogram, boxplot and density.

Example: Basic scatter plot with marginal histogram, density plot, and box plot all arranged on a page using the grid.arrange() function of the ggExtra package.




# load library tidyverse, gridExtra and ggExtra
library(tidyverse)
library(ggExtra)
library(gridExtra)
 
# set theme
theme_set(theme_bw(12))
 
# create x and y vector
xAxis <- rnorm(1000)                
yAxis <- rnorm(1000) + xAxis + 10 
 
# create sample data frame
sample_data <- data.frame(xAxis, yAxis)
 
# create scatter plot using ggplot() function
plot <- ggplot(sample_data, aes(x=xAxis, y=yAxis))+
          geom_point()+
        theme(legend.position="none")
 
# use ggMarginal function to create
# marginal histogram, boxplot and density plot
plot1 <- ggMarginal(plot, type="histogram")
plot2 <- ggMarginal(plot, type="boxplot")
plot3 <- ggMarginal(plot, type="density")
 
# combine plots in a grid
grid.arrange( plot1, plot2, plot3, ncol=3)

Output:

Marginal Plots using ggplot2

The scatter plot with marginal histograms, scatter plot with marginal boxplots, and scatter plot with marginal density plots are included in the final grid of plots, which is shown.
 

Color and size customization

We can customize the parameter of the gg-marginal () function to create the desired look for our marginal plot. We can use the size, fill, and color parameter of the gg-marginal () function to change the relative size, background fill color, and routine color of the marginal plot respectively.

Syntax: ggMarginal( plot, type, fill, color, size )

Parameters:

  • plot: Determines the base scatter plot over which marginal plot has to be added.
  • type: Determines the type of marginal plot i.e. histogram, boxplot, and density.
  • fill: Determines the background fill color of the marginal plot
  • color: Determines the outline color of the marginal plot
  • size: determines the relative size of plot elements.

Example: Here, we have modified the plot from the above example with custom colors and size parameters.




# load library tidyverse, gridExtra and ggExtra
library(tidyverse)
library(ggExtra)
library(gridExtra)
 
# set theme
theme_set(theme_bw(12))
 
# create x and y vector
xAxis <- rnorm(1000)                
yAxis <- rnorm(1000) + xAxis + 10 
 
# create sample data frame
sample_data <- data.frame(xAxis, yAxis)
 
# create scatter plot using ggplot() function
plot <- ggplot(sample_data, aes(x=xAxis, y=yAxis))+
          geom_point()+
        theme(legend.position="none")
 
# use ggMarginal function to create
# marginal histogram, boxplot and density plot
# fill, colo and size property is used for customization
plot1 <- ggMarginal(plot, type="histogram", fill= "green", size=10)
plot2 <- ggMarginal(plot, type="boxplot", color="yellow" )
plot3 <- ggMarginal(plot, type="density", color="green")
 
# combine plots in a grid
grid.arrange( plot1, plot2, plot3, ncol=3)

Output:

Marginal Plots using ggplot2

The grid is created by combining the four plots (plot 1, plot 2, plot 3, and plot) using the grid. arrange function from gridExtra. To arrange the plots in a row, the ncol option is set to 3.
 

Marginal plot for only one axis

Sometimes we need a marginal plot at only one axis either the x-axis or the y-axis. In that situation, we use the margins parameter of the gg-marginal () function. The axis where we want the marginal plot to appear is given as the value of argument margins.

Syntax: ggMarginal( plot, type, margins )

Parameters:

  • plot: Determines the base scatter plot over which marginal plot has to be added.
  • type: Determines the type of marginal plot i.e. histogram, boxplot, and density.
  • margins: Determines the axis where the marginal plot is required

Example: Here, are two plots one with the marginal plot on the x-axis other on the y-axis.




# load library tidyverse, gridExtra and ggExtra
library(tidyverse)
library(ggExtra)
library(gridExtra)
 
# set theme
theme_set(theme_bw(12))
 
# create x and y vector
xAxis <- rnorm(1000)                
yAxis <- rnorm(1000) + xAxis + 10 
 
# create sample data frame
sample_data <- data.frame(xAxis, yAxis)
 
# create scatter plot using ggplot() function
plot <- ggplot(sample_data, aes(x=xAxis, y=yAxis))+
          geom_point()+
        theme(legend.position="none")
 
# use ggMarginal function to create
# marginal histogram on x-axis
plot1 <- ggMarginal(plot, type="histogram", margins='x')
 
# use ggMarginal function to
# create marginal histogram on y-axis
plot2 <- ggMarginal(plot, type="histogram", margins='y')
 
# combine plots in a grid
grid.arrange( plot1, plot2, ncol=2)

Output:

Marginal Plots using ggplot2

The two plots (plot 1 and plot 2) are combined into a grid using the grid. arrange function from gridExtra. The plots are organized in 2 columns using the ncol option, which is set to 2. 


Article Tags :