How to Create a Scatterplot in R with Multiple Variables? - GeeksforGeeks

19 Dec, 2021
In this article, we will be looking at the way to create a scatter plot with multiple variables in the R programming language.
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 variablegfg_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 variablegfg_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_y1plot(gfg_x1,gfg_y1, col='darkgreen', pch=19)  # Adding scatterplot of gfg_x2 vs gfg_y2points(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 variablegfg_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 variablegfg_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 variablegfg_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 Forth variablegfg_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_y1plot(gfg_x1,gfg_y1, col='darkgreen', pch=19)  # Adding scatterplot of gfg_x2 vs gfg_y2points(gfg_x2, gfg_y2, col='red', pch=19)  # Adding scatterplot of gfg_x3 vs gfg_y3points(gfg_x3, gfg_y3, col='blue', pch=19)  # Adding scatterplot of gfg_x4 vs gfg_y4points(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 variablegfg_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 variablegfg_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 variablegfg_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 Forth variablegfg_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 variablegfg_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 variablegfg_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_y1plot(gfg_x1,gfg_y1, col='darkgreen', pch=19)  # Adding scatterplot of gfg_x2 vs gfg_y2points(gfg_x2, gfg_y2, col='red', pch=19)  # Adding scatterplot of gfg_x3 vs gfg_y3points(gfg_x3, gfg_y3, col='blue', pch=19)  # Adding scatterplot of gfg_x4 vs gfg_y4points(gfg_x4, gfg_y4, col='orange', pch=19)  # Adding scatterplot of gfg_x5 vs gfg_y5points(gfg_x5, gfg_y5, col='purple', pch=19)  # Adding scatterplot of gfg_x6 vs gfg_y6points(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:
Picked
R-Charts
R-Graphs
R-plots
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
Data Visualization in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
Logistic Regression in R Programming
How to Split Column Into Multiple Columns in R DataFrame?
Control Statements in R Programming
How to import an Excel File into R ?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?