當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 lines()用法及代碼示例


lines()R 語言中的函數用於向現有繪圖添加不同類型、顏色和寬度的線條。

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

參數:
x, y:坐標向量
col:線條顏色
lwd:線寬
lty:線路類型

樣本散點圖:


# 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")



範例1:


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

輸出:

範例2:


# 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(2, 3, 4, 5)
y2 <- c(2, 3, 4, 5)
  
# Plotting a line
lines(x2, y2, col = "red", lwd = 3, lty = 2)

輸出:




相關用法


注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Addition of Lines to a Plot in R Programming – lines() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。