How to plot scattermatrix in R?

This recipe helps you plot scattermatrix in R

Recipe Objective

A scatter matrix or a scatter plot matrix is a grid which consists of NxN scatter plot. A scattermatrix is mainly used to display the bivariate relationships among all the pairs of variables in the dataset through scatterplots. This allows us to explore all the relationships between pairs in a single graph. ​

Minimum three numeric variables are needed to plot a scatter matrix. The layout of the matrix consists of a upper right half and left lower half cut across a diagonal. ​

This recipe demonstrates how to plot a scattermatrix in R. ​

Hands-On Guide to the Art of Tuning Locality Sensitive Hashing in Python

STEP 1: Loading required library and dataset

Dataset description: It is the basic data about the customers going to the supermarket mall. The variables that we are interested in: Annual.Income (which is in 1000s) , Spending Score and Age

# Data manipulation package library(dplyr) library(tidyverse) # reading a dataset customer_seg = read.csv('R_121_Mall_Customers.csv') # selecting the required variables using the select() function customer_seg_var = select(customer_seg, Age, Annual.Income..k..,Spending.Score..1.100.) # summary of the selected variables glimpse(customer_seg_var)

Observations: 200
Variables: 3
$ Age                     19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, 35…
$ Annual.Income..k..      15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, 19…
$ Spending.Score..1.100.  39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99, 1…
</pre

STEP 2: Plotting a scatter matrix

We use pairs() function to plot a scatter matrix.

Syntax: pairs(x, col = , pch = , labels = , main = )

Where:

  1. x = dataframe
  2. col = used to change the colour of the points
  3. pch = used to change the shape of the points
  4. labels = used to change the labels of the diagnol
  5. main = used to give a title to the graph

pairs(customer_seg_var,
      col = "green",                                    
      pch = 19,                                            
      labels = c("Age", "Annual Income", "Spending Score"),                  
      main = "Scatter Matrix")
 ​

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Build a Medical AI Assistant using Unsloth and QLoRA
In this AI Project, you will learn to fine-tune the LLaMA 3.1 8B model using Unsloth and QLoRA to build a domain-specific medical AI assistant capable of accurate, context-aware, and memory-efficient clinical conversations. It also integrates a Streamlit chatbot interface for real-time interaction and deployment.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Build and Deploy an AI Resume Analyzer with OpenAI and Azure
In this AI Resume Analyzer project, you will learn to build and deploy AI resume analyzer that helps job seekers assess how effectively their resumes match job descriptions using OpenAI's language models and Azure's cloud infrastructure.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .