Draw Scatter Plot with two Nominal Variables with Plotly Package in R
Last Updated :
26 Apr, 2025
A scatter plot is a visual representation of the relationship between two variables. It is commonly used to identify patterns and trends in data. In this article, we will learn how to create a scatter plot with two nominal variables using the Plotly package in R Programming Language.
- Nominal variables: Nominal variables are categorical variables that do not have a natural order or ranking. For example, hair color (brown, black, blonde) and eye color (brown, blue, green) are nominal variables.
- Continuous variables: Continuous variables are numerical values that can take any value within a certain range, as opposed to discrete variables that can only take specific, separated values. Examples of continuous variables include height, weight, and temperature.
Plotly: Plotly is a data visualization library that allows you to create interactive charts and graphs in R.
Steps Needed to Create a Scatter Plot
To create a scatter plot with two nominal variables using Plotly in R, follow these steps. The first step is to install the Plotly package and after installing the Plotly package, you need to load it.
R
install.packages("plotly")
library(plotly)
Use the plot_ly() function to create the scatter plot. Specify the x and y variables, and set the type option to "scatter". You can also use the mode option to control the appearance of the points. For example, to create a scatter plot with x on the x-axis and y on the y-axis.
Syntax:Â
plot_ly(x, y, type, mode, size, color)
Where,
- x - ~carat, - this sets the variable "carat" to be plotted on the x-axis. The tilde (~) is used to indicate that the variable is unquoted and should be evaluated as a variable.
- y - ~price, - this sets the variable "price" to be plotted on the y-axis.
- type - "scatter", - this sets the type of chart to be a scatter plot.
- mode - "markers", - this sets the mode of the chart to "markers," which means that individual data points will be represented by markers (e.g., circles, squares, etc.).
- size - this sets the size of the markers
- color - ~cut - this sets the variable "cut" to be used for coloring the markers.
Customize the appearance of the plot by adding additional options. For example, you can use the layout() function to add a title and axis labels.
R
layout(title = "Scatter Plot", xaxis = list(title = "X-axis Label"),
yaxis = list(title = "Y-axis Label"))
Scatter Plot with Nominal Variables
We will use the mtcars dataset and create a scatter plot with cyl on the x-axis and gear on the y-axis. We will use the type = "scatter" option to create a scatter plot, and the mode = "markers + lines" option to add lines connecting the points. This will create a scatter plot with cyl on the x-axis and gear on the y-axis. The points will be connected by lines.
R
# Load the Plotly package
library(plotly)
# Load the mtcars dataset
data <- mtcars
# Create the scatter plot
plot <- plot_ly(mtcars, x = ~cyl, y = ~gear,
type = "scatter",
mode = "markers+lines")
# Customize the plot
plot <- plot %>%
layout(title = "Scatter Plot with Nominal Variables",
xaxis = list(title = "Cylinders"),
yaxis = list(title = "Gears"))
# Display the plot
plot
Output:
Scatter Plot with Nominal VariablesScatter Plot with Nominal Variables
We'll use the airquality dataset, which contains information about the air quality in New York City. We'll create a scatter plot with the Ozone variable on the x-axis and the Wind variable on the y-axis. This will create a scatter plot with Ozone on the x-axis and Wind on the y-axis.
R
# Load the Plotly package
library(plotly)
# Load the mtcars dataset
data <- airquality
# Create the scatter plot
plot <- plot_ly(airquality, x = ~Ozone,
y = ~Wind, type = "scatter",
mode = "markers")
# Customize the plot
plot <- plot %>%
layout(title = "Scatter Plot with Continuous Variables",
xaxis = list(title = "Ozone"),
yaxis = list(title = "Wind"))
# Display the plot
plot
Output:
Scatter Plot with Nominal Variables
Now let's draw one more scatter plot using the diamonds dataset. In this dataset, we will draw a scatter plot of carat vs price by cut.
R
# Load the Plotly package
library(plotly)
# Load the diamonds dataset
data <- diamonds[sample(nrow(diamonds), 100),]
# Create the scatter plot
plot <- plot_ly(data, x = ~carat,
y = ~price, type = "scatter",
mode = "markers", size = 10,
color = ~cut) %>%
layout(xaxis = list(title = "Carat"),
yaxis = list(title = "Price"),
title = "Scatter plot of Carat vs Price by Cut")
# Display the plot
plot
Output:
Scatter Plot of Carat vs Price by Cut
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Random Forest Algorithm in Machine Learning
A Random Forest is a collection of decision trees that work together to make predictions. In this article, we'll explain how the Random Forest algorithm works and how to use it.Understanding Intuition for Random Forest AlgorithmRandom Forest algorithm is a powerful tree learning technique in Machine
7 min read
What is a Neural Network?
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamenta
14 min read