axis函數用來在一張圖表上添加軸線,區別於傳統的x軸和y軸,axis 允許在上,下,左, 右4個方向添加軸線
以x軸為例,一條軸線包含3個元素,水平的一條橫線,叫做axis line , 刻度線, 叫做tick line, 對應的標簽 labels
基本用法:
通過side 參數設置需要添加的軸線的方向,從下邊開始,沿逆時針方向,數字為1到4
代碼示例:
par(oma = c(1, 1, 1, 1), mfrow = c(1, 4)) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) text(x = 3, y = 3, labels = "side = 1", cex = 2) box() axis(side = 1) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) text(x = 3, y = 3, labels = "side = 2", cex = 2) box() axis(side = 2) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) text(x = 3, y = 3, labels = "side = 3", cex = 2) box() axis(side = 3) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) text(x = 3, y = 3, labels = "side = 4", cex = 2) box() axis(side = 4)
效果圖如下:
參數設置:
at : 需要添加刻度的數值,默認會根據變量的取值范圍計算幾個合適的刻度,也可以手工指定
代碼示例:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) box() axis(side = 1, at = c(0, 2, 4, 6))
效果圖如下:
lables : 指定在刻度上需要標記的內容,默認就是刻度對應的值
代碼示例:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) box() axis(side = 1, at = c(0, 2, 4, 6), labels = paste(c(0, 2, 4, 6), "A", sep = ""))
效果圖如下:
tick : 邏輯值,是否顯示軸線,包括刻度線和對應的軸線, FALSE 表示不顯示
代碼示例:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) axis(side = 1, tick = F)
效果圖:
line : 軸線的位置
代碼示例:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) box() axis(side = 1, line = 1)
效果圖如下:
lwd : 設置 axis line 和 tick line 的寬度
代碼示例:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) box() axis(side = 1, line = 1, lwd = 2)
效果圖如下:
lwd.tick : 設置tick line的寬度
代碼示例:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) box() axis(side = 1, line = 1, lwd = 1, lwd.tick = 2)
效果圖如下:
lty : 設置axis line 和tick line的線條類型
代碼示例:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) box() axis(side = 1, line = 1, lty = 3)
效果圖如下:
col : 設置axis line 和 tick.line 的線條顏色
代碼示例:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) box() axis(side = 1, line = 1, col = "blue")
效果圖如下:
col.ticks : 設置tick line的顏色
代碼示例:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) box() axis(side = 1, line = 1, col = "blue", col.ticks = "red")
效果圖如下:
pos : 對軸線的位置進行調整,當pos 設置了對應的值之后會覆蓋line 參數的值
代碼示例:
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", axes = F) box() axis(side = 1, pos = 1)
效果圖如下: