R语言 为绘图添加线条 – lines()函数

R语言 为绘图添加线条 – lines()函数

R编程语言 中的lines()函数 用于在现有绘图中添加不同类型、颜色和宽度的线条。

语法: lines(x, y, col, lwd, lty)

参数

  • x, y: 坐标矢量
  • col: 线条的颜色
  • lwd: 线条的宽度
  • lty: 线条的类型

在R中使用lines()函数向图中添加线条

用于 演示的 散点图样本

这里我们将使用数据集创建一个散点图。

# R program to create a scatter plot
 
# Creating coordinate vectors
x <- c(1.3, 3.5, 1.4, -3.1, 5.7, 2.4,
       3.3, 2.5, 2.3, 1.9, 1.8, 2.3)
y <- c(2.5, 5.8, 2.1, -3, 12, 5, 6.2,
       4.8, 4.2, 3.5, 3.7, 5.2)
 
# Plotting the graph
plot(x, y, cex = 1, pch = 3,
     xlab ="x", ylab ="y",
     col ="black")

输出

在R编程中为绘图添加线条 - lines()函数

例1: 使用lines()函数为散点添加直线。

# R program to add lines into plots
 
# Creating coordinate vectors
x <- c(1.3, 3.5, 1.4, -3.1, 5.7,
       2.4, 3.3, 2.5, 2.3, 1.9, 1.8, 2.3)
y <- c(2.5, 5.8, 2.1, -3, 12, 5,
       6.2, 4.8, 4.2, 3.5, 3.7, 5.2)
 
# Plotting the graph
plot(x, y, cex = 1, pch = 3, xlab ="x",
     ylab ="y", col ="black")
 
# Creating coordinate vectors
x2 <- c(4.3, 1.2, -2.5, -0.4)
y2 <- c(3.5, 4.6, 2.5, 3.2)
 
# Plotting a line
lines(x2, y2, col = "red",
      lwd = 2, lty = 1)

输出

在R编程中为绘图添加线条 - lines()函数

例2:在R语言中使用点来向图中添加点

这里我们将创建一个散点图,然后用lines()函数连接线。

# R program to add lines into plots
 
# Creating coordinate vectors
x <- c(1.3, 3.5, 1.4, -3.1, 5.7, 2.4,
       3.3, 2.5, 2.3, 1.9, 1.8, 2.3)
y <- c(2.5, 5.8, 2.1, -3, 12, 5, 6.2,
       4.8, 4.2, 3.5, 3.7, 5.2)
 
# Plotting the graph
plot(x, y, cex = 1, pch = 3,
     xlab ="x", ylab ="y",
     col ="black")
 
lines(x, y, col = "red")

输出

在R编程中为绘图添加线条 - lines()函数

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程