Open In App

Scatter Slot using Plotly in R

In order to examine the relationship between two variables in data analysis, scatter plots are a fundamental visualisation tool. When we wish to visualize the distribution of data points and search for patterns, trends, or outliers, they are extremely helpful. With the help of the potent R package Plotly, we can make interactive scatter plots that look good. This post will offer a thorough tutorial on producing scatter plots in R using Plotly.

Scatter Plots

The values for two different numerical variables are represented by dots in a scatter plot (also known as a scatter chart or scatter graph). Each dot’s location on the horizontal and vertical axes represents a data point’s values. To view relationships between variables, utilise scatter plots.



Uses of Scatter Plots

Plotly Package

The plotly package in R is a powerful and versatile library for creating interactive and visually appealing data visualizations. It is built on top of the JavaScript library Plotly.js, which allows you to create interactive web-based charts and plots. plotly in R enables you to create a wide range of plots, including scatter plots, line charts, bar plots, heatmaps, and more, with the added advantage of interactivity.

Pre-Requisites

Before moving forward make sure you have plotly package installed.



install.packages("plotly")

Scatter Plots in R using Plotly

Loading the package




library(plotly)

Create Basic Scatter Plot




# Sample data
x <- c(1, 2, 3, 4, 5)
y <- c(2, 3, 5, 4, 7)
 
# Create a basic scatter plot
plot_ly(x = x, y = y, type = 'scatter', mode = 'markers')

Output:

scatter plot using plotly in R

Customizing Scatter Plots




# Create a scatter plot with labels and a title
plot_ly(x = x, y = y, type = 'scatter', mode = 'markers') %>%
  layout(
    xaxis = list(title = 'X-axis'),
    yaxis = list(title = 'Y-axis'),
    title = 'Customized Scatter Plot'
  )

Output:

Customized Scatter Plot

Changing Marker Color and Size




# Customize marker color and size
plot_ly(x = x, y = y, type = 'scatter', mode = 'markers',
        marker = list(color = 'red', size = 10))

Output:

scatter plot using plotly in R

Adding Regression Line




# Add a regression line
plot_ly(x = x, y = y, type = 'scatter', mode = 'markers') %>%
  add_trace(
    x = x,
    y = lm(y ~ x)$fitted.values,
    mode = 'lines',
    line = list(color = 'blue'),
    name = 'Regression Line'
  )

Output:

Adding Regression Line

Multiple Scatter Plots




x1 <- c(1, 2, 3, 4, 5)
y1 <- c(2, 3, 5, 4, 7)
 
x2 <- c(1, 2, 3, 4, 5)
y2 <- c(3, 4, 2, 6, 5)
 
 
# Create multiple scatter plots
plot_ly(x = x1, y = y1, type = 'scatter', mode = 'markers', name = 'Dataset 1')%>%
  add_trace(x = x2, y = y2, type = 'scatter', mode = 'markers', name = 'Dataset 2')

Output:

Multiple Scatter Plots

3D Scatter Plot




# Sample data
x <- c(1, 2, 3, 4, 5)
y <- c(2, 3, 5, 4, 7)
z <- c(10, 8, 12, 9, 15)
categories <- c("A", "B", "A", "C", "B")
 
# Define colors for categories
category_colors <- c("red", "blue", "green")
 
# Create a 3D scatter plot with colored points
plot_ly(x = x, y = y, z = z, type = 'scatter3d', mode = 'markers',
        marker = list(color = factor(categories, levels = unique(categories),
                                     labels = category_colors)))

Output:

3D scatter plot

Conclusion

This article thoroughly examines how to create interactive scatter plots in R using the Plotly package. Scatter plots are essential for finding data trends and visualising correlations between variables. Plotly, which is based on the Plotly.js framework, enables R users to construct engaging data visualisations. The course goes over crucial aspects of scatter plot building, customization, and interaction to improve data understanding. For data professionals, learning how to make dynamic scatter plots with Plotly is an important skill that will help with effective data analysis and communication.


Article Tags :