Open In App

How to Create a Scatterplot in R with Multiple Variables?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be looking at the way to create a scatter plot with multiple variables in the R programming language.

 Using Plot() And Points() Function In Base R:

In this approach to create a scatter plot with multiple variables, the user needs to call the plot() function

  • Plot() function: This is a generic function for the plotting of R objects.

Syntax:

plot(x, y, …)

Parameters:

  • x: the x coordinates of points in the plot.
  • y: the y coordinates of points in the plot

Points() function: It is a generic function to draw a sequence of points at the specified coordinates

Syntax:

points(x,y = NULL, type = “p”, …)

Parameters:

  • x, y: coordinate vectors of points to plot.
  • type: character indicating the type of plotting; actually any of the types as in plot.default.
  • …: Further graphical parameters may also be supplied as arguments.

Example 1:

In this example, we will be creating a scatter plot of 2 different variables using the plot() and the point() function in the R programming language. 

R




# Creating First variable
gfg_x1 = c(9,1,8,7,7,3,2,4,5,6)
gfg_y1 = c(7,4,1,5,9,6,3,3,6,9)
 
# Creating Second variable
gfg_x2 = c(4,1,5,9,7,4,5,2,8,4)
gfg_y2 = c(9,1,5,7,4,1,3,6,5,2)
 
# creating scatterplot of gfg_x1 vs. gfg_y1
plot(gfg_x1,gfg_y1, col='darkgreen', pch=19)
 
# Adding scatterplot of gfg_x2 vs gfg_y2
points(gfg_x2, gfg_y2, col='red', pch=19)
 
legend(1,9,legend=c('Variable 1', 'Variable 2'),
       pch=c(19, 19), col=c('darkgreen', 'red'))


Output:

Example 2:

Here, we will be creating a scatter plot of 4 different variables.

R




# Creating First variable
gfg_x1 = c(9,1,8,7,7,3,2,4,5,6)
gfg_y1 = c(7,4,1,5,9,6,3,3,6,9)
 
# Creating Second variable
gfg_x2 = c(4,1,5,9,7,4,5,2,8,4)
gfg_y2 = c(9,1,5,7,4,1,3,6,5,2)
 
# Creating Third variable
gfg_x3 = c(6,8,5,7,4,1,6,3,2,9)
gfg_y3 = c(7,4,6,1,5,6,3,5,4,1)
 
# Creating Fourth variable
gfg_x4 = c(1,8,7,5,6,3,2,4,5,6)
gfg_y4 = c(2,5,8,6,5,8,6,9,2,1)
 
 
# creating scatterplot of gfg_x1 vs. gfg_y1
plot(gfg_x1,gfg_y1, col='darkgreen', pch=19)
 
# Adding scatterplot of gfg_x2 vs gfg_y2
points(gfg_x2, gfg_y2, col='red', pch=19)
 
# Adding scatterplot of gfg_x3 vs gfg_y3
points(gfg_x3, gfg_y3, col='blue', pch=19)
 
# Adding scatterplot of gfg_x4 vs gfg_y4
points(gfg_x4, gfg_y4, col='orange', pch=19)
 
legend('topleft',legend=c('Variable 1', 'Variable 2','Variable 3','Variable 4'),
       pch=c(19, 19), col=c('darkgreen', 'red','blue','orange'))


Output:

Example 3:

Here, we will be creating a scatter plot of 6 different variables.

R




# Creating First variable
gfg_x1 = c(9,1,8,7,7,3,2,4,5,6)
gfg_y1 = c(7,4,1,5,9,6,3,3,6,9)
 
# Creating Second variable
gfg_x2 = c(4,1,5,9,7,4,5,2,8,4)
gfg_y2 = c(9,1,5,7,4,1,3,6,5,2)
 
# Creating Third variable
gfg_x3 = c(6,8,5,7,4,1,6,3,2,9)
gfg_y3 = c(7,4,6,1,5,6,3,5,4,1)
 
# Creating Fourth variable
gfg_x4 = c(1,8,7,5,6,3,2,4,5,6)
gfg_y4 = c(2,5,8,6,5,8,6,9,2,1)
 
# Creating Fifth variable
gfg_x5 = c(8,9,5,6,2,4,4,6,4,1)
gfg_y5 = c(3,5,7,4,5,6,4,6,5,7)
 
# Creating Sixth variable
gfg_x6 = c(4,5,6,3,2,2,5,5,9,6)
gfg_y6 = c(7,8,5,6,3,5,9,4,5,7)
 
# creating scatterplot of gfg_x1 vs. gfg_y1
plot(gfg_x1,gfg_y1, col='darkgreen', pch=19)
 
# Adding scatterplot of gfg_x2 vs gfg_y2
points(gfg_x2, gfg_y2, col='red', pch=19)
 
# Adding scatterplot of gfg_x3 vs gfg_y3
points(gfg_x3, gfg_y3, col='blue', pch=19)
 
# Adding scatterplot of gfg_x4 vs gfg_y4
points(gfg_x4, gfg_y4, col='orange', pch=19)
 
# Adding scatterplot of gfg_x5 vs gfg_y5
points(gfg_x5, gfg_y5, col='purple', pch=19)
 
# Adding scatterplot of gfg_x6 vs gfg_y6
points(gfg_x6, gfg_y6, col='black', pch=19)
 
legend('topleft',legend=c('Variable 1', 'Variable 2','Variable 3','Variable 4',
                          'Variable 5','Variable 6'), pch=c(19, 19),
       col=c('darkgreen', 'red','blue','orange','purple','black'))


Output:



Last Updated : 14 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads