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.

R




# 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 code begins by loading the required packages: tidyverse for data manipulation, ggExtra for additional ggplot functionalities, and gridExtra for arranging multiple plots in a grid.
     
  • The theme_set() function sets the theme to “theme_bw” with a base font size of 12, determining the visual style of the plots.
     
  • Two vectors, xAxis and yAxis, are created using the rnorm() function. These vectors represent the x and y coordinates for the scatter plot.
     
  • A sample data frame named sample_data is created using the data.frame() function, combining the xAxis and yAxis vectors.
     
  • The ggplot() method is used to produce the scatter plot. The aes() function is used to map the xAxis variable to the x-axis and the yAxis variable to the y-axis. The sample_data data frame is supplied as the data source. The scatter plot’s data points are represented by additional points added using the geom_point() function.
     
  • To alter the plot’s visual style, use the theme() function. Legend. position = “none” is used in this instance to remove the legend from the plot.
     
  • Three further plots are produced using the ggMarginal() function from the ggExtra package: plot1, plot2, and plot3. The type option is set to “density” for plot 3, “boxplot” for plot 2, and “histogram” for plot1. Based on the data, these functions produce density plots, boxplots, and marginal histograms, respectively.
     
  • The plots of a grid are combined using the grid.arrange() function from the gridExtra library. Plot1, Plot2, and Plot3 are supplied as parameters, and ncol = 3 indicates that the grid should contain three columns.

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.
 

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.

Similar Reads

Installation:

To install the ggExtra package we use:...

Creation of Marginal plots

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

Color and size customization

...

Marginal plot for only one axis

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....