R 語言中的 segment() 函數用於在特定點之間繪製線段。
用法: segments(x0, y0, x1, y1)
參數:
x, y:坐標以在提供的點之間繪製線段。
返回:給定點之間的線段
範例1:繪製單個線段
# Create empty example plot
plot(0, 0, col = "white", xlab = "", ylab = "")
# Draw one line
segments(x0 = - 1, y0 = - 0.5, x1 = 0.5, y1 = 0, col = "darkgreen")
輸出:
這裏,x0 & y0 是線段的起點,x1 & y1 是線段的終點。
範例2:修改顏色、粗細和線型
# Create empty example plot
plot(0, 0, col = "white", xlab = "", ylab = "")
# Draw one line as in Example 1
segments(x0 = - 1, y0 = - 1, x1 = 0.5, y1 = 0.5,
# Color of line
col = "darkgreen",
# Thickness of line
lwd = 5,
# Line type
lty = "dotted")
輸出:
範例3:繪製多條線段到 R Plot。
# Create empty example plot
plot(0, 0, col = "white", xlab = "", ylab = "")
# Create data frame with line-values
multiple_segments <- data.frame(x0 = c(0.1, 0.2, - 0.7, 0.4, - 0.8),
y0 = c(0.8, 0.3, 0.5, - 0.4, 0.3),
x1 = c(0, 0.4, 0.5, - 0.5, - 0.7),
y1 = c(- 0.3, 0.4, - 0.5, - 0.7, 0.8))
# Draw multiple lines
segments(x0 = multiple_segments$x0,
y0 = multiple_segments$y0,
x1 = multiple_segments$x1,
y1 = multiple_segments$y1)
輸出:
相關用法
- R語言 polygon()用法及代碼示例
- R語言 points()用法及代碼示例
- R語言 arrows()用法及代碼示例
- R語言 curve()用法及代碼示例
- R語言 between()用法及代碼示例
注:本文由純淨天空篩選整理自kaurbal1698大神的英文原創作品 Draw Line Segments between Particular Points in R Programming – segments() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。