R 語言中的 polygon() 函數用於在現有繪圖中的指定點之間繪製多邊形。
用法: polygon(x_coordinates, y_coordinates)
參數:
x_coordinates, y_coordinates:繪製多邊形的圖的 x, y 坐標
返回:給定圖中的多邊形
範例1:在 R 圖中繪製方形多邊形
# R program to draw a polygon
# Draw an empty plot
plot(2, 2, col = "white", xlab = "X", ylab = "Y")
# Draw a polygon
polygon(x = c(2.7, 2.3, 2.2, 2.8), # X-Coordinates of polygon
y = c(2.6, 2.8, 2.4, 2), # Y-Coordinates of polygon
col = "darkgreen")
輸出:
範例2:多邊形的顏色邊界
# R program to draw a polygon
# Draw empty plot
plot(2, 2, col = "white", xlab = "X", ylab = "Y")
# Draw a polygon
polygon(x = c(2.7, 2.3, 2.2, 2.8), # X-Coordinates of polygon
y = c(2.6, 2.8, 2.4, 2), # Y-Coordinates of polygon
col = "darkgreen", # Color of polygon
border = "red", # Color of polygon border
lwd = 8) # Thickness of border
輸出:
這裏,border 指定邊框顏色,lwd 指定邊框粗細。
範例3:繪製頻率多邊形
# R program to draw a polygon
# X values for frequency polygon
x1 <- 1:10
# Y values for frequency polygon
y1 <- c(2, 4, 7, 4, 5, 8, 6, 6, 1, 2)
# Plot frequency polygon
plot(x1, y1,
type = "l", # Set line type to line
lwd = 4) # Thickness of line
# X-Y-Coordinates of polygon
polygon(c(1, x1, 10), c(0, y1, 0),
col = "darkgreen") # Color of polygon
# Add squares to frequency polygon
points(x1, y1,
cex = 1, # Size of squares
pch = 12)
segments(x1, 0, x1, y1)
輸出:
範例4:在密度以下繪製多邊形
# R program to draw a polygon
# Set seed for reproducibility
set.seed(15000)
# Sample size
N <- 1000
# Draw random poisson distribution
x1 <- rpois(N, 2)
plot(density(x1), # Draw density plot
main = "", # No main title
xlab = "x1") # Set name of x-axis to x2
# X-Coordinates of polygon
polygon(c(min(density(x1)$x), density(x1)$x),
c(0, density(x1)$y), # Y-Coordinates of polygon
col = "darkgreen") # Color of polygon
輸出:
在這裏,上麵的例子是用來製作概率密度函數的。
相關用法
- R語言 segments()用法及代碼示例
- R語言 points()用法及代碼示例
- R語言 colorRampPalette()用法及代碼示例
- R語言 arrows()用法及代碼示例
- R語言 arrayInd()用法及代碼示例
- R語言 word()用法及代碼示例
- R語言 get()用法及代碼示例
- R語言 curve()用法及代碼示例
- R語言 signif()用法及代碼示例
- R語言 strtrim()用法及代碼示例
- R語言 exists()用法及代碼示例
- R語言 gl()用法及代碼示例
- R語言 rep_len()用法及代碼示例
- R語言 rep.int()用法及代碼示例
- R語言 seq_len()用法及代碼示例
- R語言 seq.int()用法及代碼示例
- R語言 rgb()用法及代碼示例
注:本文由純淨天空篩選整理自kaurbal1698大神的英文原創作品 Draw a Polygon between specified points in R Programming – polygon() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。