How to create a marginal plot with ggplot2 in R

This recipe helps you create a marginal plot with ggplot2 in R

Recipe Objective

A marginal plot is an extension of scatter plot which not only shows the relationship between two variables but also shows the individual distributions such as Histogram, Box plots of the variables. ​

In this recipe we are going to use ggplot2 as well as ggExtra package to plot the required Marginal plot. ggplot2 package is based on the book Grammar of Graphics by Wilkinson. This package provides flexibility while incorporating different themes and plot specification with a high level of abstraction. The package mainly uses aesthetic mapping and geometric objects as arguments. Different types of geometric objects include: ​

  1. geom_point() - for plotting points
  2. geom_bar() - for plotting bar graph
  3. geom_line() - for plotting line chart
  4. geom_histogram() - for plotting histogram

The basic syntax of gggplot2 plots is: ​

ggplot(data, mapping = aes(x =, y=)) + geometric object ​

where: ​

  1. data : Dataframe that is used to plot the chart
  2. mapping = aes() : aesthetic mapping which deals with controlling axis (x and y indicates the different variables)
  3. geometric object : Indicates the code for typeof plot you need to visualise.

ggExtra Package is mainly used to further enhance the built-in features of "ggplot2" by providing us with various functions to create Marginal plots especially Histogram and Boxplot.

This recipe demonstrates how to make a Marginal Plots using ggplot2 and ggExtra.

STEP 1: Loading required library and dataset

We will take an example of normal distribution along x and y axis and showcase the individual histogram and boxplot of the same.

# ggplot for data visualisation library(ggplot2) # installing ggExtra for marginal plots using devtools install.packages("ggExtra") library(ggExtra) # Creating a dataframe of normally distributed 1000 points with mean = 25 and std.dev = 5 norm_dist = data.frame(x = rnorm(1000, 25, 5), y = rnorm(1000, 25, 5)) norm_dist
x	y
20.08595	27.34024
24.57561	33.94111
31.76342	33.02787
31.81128	15.18382
23.61155	31.49135
...	...
24.98564	27.50006
25.33502	32.35901
26.72924	29.06489
23.44376	22.31388
20.21346	19.30083

STEP 2: Plotting a scatter plot using ggplot

We use geometric object as geom_point() to plot a scatter plot (x vs y)

Note:

  1. The + sign in the syntax earlier makes the code more readable and enables R to read further code without breaking it.
  2. We also use labs() function to give a title to the graph
scat_plot = ggplot(norm_dist, mapping = aes(x, y )) + geom_point() + labs(title = "X vs Y") scat_plot

STEP 3: Marginal Plot- Histogram and Boxplot

We use "ggExtra::ggMarginal()" function to plot a marginal plot.

syntax: ggExtra::ggMarginal(x , type = )

where:

  1. x = plot on which the marginal plot is created
  2. type = type of marginal plot
# Marginal Histogram plot ggExtra::ggMarginal(scat_plot, type="histogram") # Marginal Boxplot ggExtra::ggMarginal(scat_plot, type="boxplot")

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.