📜  如何在 R 中创建具有多个变量的散点图?

📅  最后修改于: 2022-05-13 01:54:55.126000             🧑  作者: Mango

如何在 R 中创建具有多个变量的散点图?

在本文中,我们将研究在 R 编程语言中创建具有多个变量的散点图的方法。

在 Base R 中使用 Plot() 和 Points()函数:

在这种创建具有多个变量的散点图的方法中,用户需要调用 plot()函数

  • Plot()函数:这是用于绘制 R 对象的通用函数。

Points()函数:在指定坐标处绘制一系列点的通用函数

示例 1:

在此示例中,我们将使用 R 编程语言中的 plot() 和 point()函数创建 2 个不同变量的散点图。

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'))


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 Forth 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'))


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 Forth 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'))


输出:

示例 2:

在这里,我们将创建 4 个不同变量的散点图。

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 Forth 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'))

输出:

示例 3:

在这里,我们将创建一个包含 6 个不同变量的散点图。

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 Forth 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'))

输出: