arrows 函數用來在一張圖表上添加箭頭,只需要分別指定起始坐標和終止坐標,就可以添加箭頭了,還可以通過一些屬性對箭頭的形狀,大小進行調整
基本用法:
xo, yo 指定起始點的x和y坐標,x1, y1 指定終止點的x和y坐標, 代碼示例如下:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4)
效果如下:
x0, y0,x1,y1 支持一次設置多個值,同時畫多個箭頭,示例代碼如下:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n") arrows(x0 = c(1, 1), y0 = c(1, 2), x1 = c(4, 4), y1 = c(4, 5))
效果如下:
參數調整:
length : 該參數一次只能設置一個值,默認值為0.25, 為了調整不同箭頭的大小,建議分別設置,用法如下:
par(mfrow = c(1,3)) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "length = 0.1") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4, length = 0.1) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "length = 0.5") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4, length = 0.5) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "length = 1") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4, length = 1)
效果如下:
code : 調整箭頭的類型,一共有1,2,3,4 共四種類型,該參數一次只能設置一個值,四種類型具體可以看下面的效果圖
代碼如下:
par(mfrow = c(1,3)) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "code = 1") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4, code = 1) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "code = 2") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4, code = 2) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "code = 3") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4, code = 3)
效果圖如下:
code = 1 代表箭頭由終止點指向起始點
code = 2 是默認值,箭頭由起始點指向終止點
code = 3 代表在起始點和終止點兩端都標上箭頭
angle : 設置箭頭的角度,默認值是45,該參數一次只能設置一個值,代碼如下:
par(mfrow = c(1,3)) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "angle = 15") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4, length = 0.5, angle = 15) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "angle = 45") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4, length = 0.5, angle = 45) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "angle = 60") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4, length = 0.5, angle = 60)
效果圖如下:
除了上面的針對arrows 的特殊參數之外,也支持一些通用的參數,col , lty ,lwd 等
代碼如下:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n") arrows(x0 = 1, y0 = 1, x1 = 4, y1 = 4, col = "red", lwd = 2, lty = 3)
效果圖如下: