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


R grid 將網格添加到繪圖中

R語言 grid 位於 graphics 包(package)。

說明

gridnxny 矩形網格添加到現有繪圖中。

用法

grid(nx = NULL, ny = nx, col = "lightgray", lty = "dotted",
     lwd = par("lwd"), equilogs = TRUE)

參數

nx , ny

x 和 y 方向上網格的單元數。當 NULL 時,默認情況下,網格與相應默認軸上的刻度線對齊(即由 axTicks 計算的刻度線)。當 NA 時,相應方向不繪製網格線。

col

字符或(整數)數字;網格線的顏色。

lty

字符或(整數)數字;網格線的線型。

lwd

給出網格線寬度的非負數字。

equilogs

邏輯,僅當日誌坐標和與軸刻度線的對齊處於活動狀態時使用。在這種情況下設置equilogs = FALSE會給出非等距刻度對齊的網格線。

注意

如果需要更精細的調整,請直接使用abline(h = ., v = .)

例子

plot(1:3)
grid(NA, 5, lwd = 2) # grid only in y-direction

## maybe change the desired number of tick marks:  par(lab = c(mx, my, 7))
op <- par(mfcol = 1:2)
with(iris,
     {
     plot(Sepal.Length, Sepal.Width, col = as.integer(Species),
          xlim = c(4, 8), ylim = c(2, 4.5), panel.first = grid(),
          main = "with(iris,  plot(...., panel.first = grid(), ..) )")
     plot(Sepal.Length, Sepal.Width, col = as.integer(Species),
          panel.first = grid(3, lty = 1, lwd = 2),
          main = "... panel.first = grid(3, lty = 1, lwd = 2), ..")
     }
    )
par(op)

plot(1:64)
gr <- grid() # now *invisibly* returns the grid "at" locations
str(gr)
stopifnot(length(gr) == 2, identical(gr[[1]], gr[[2]]),
          gr[["atx"]] == 10*(0:6))

## In log-scale plots :
plot(8:270, log="xy") ; grid() # at (1, 10, 100); if preferring "all" grid lines:
plot(8:270, log="xy") ; grid(equilogs = FALSE) -> grll
stopifnot(identical(grll, list(atx = c(1, 2, 5, 10, 20, 50, 100, 200),
                               aty = c(         10, 20, 50, 100, 200))))

參考

Murrell, P. (2005) R Graphics. Chapman & Hall/CRC Press.

也可以看看

plotablinelinespoints

相關用法


注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Add Grid to a Plot。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。