为运动场添加连续编号
Add consecutive numbering to playing field
我正在使用 R 控制台创建一个小游戏,我正在尝试向我的空白游戏场添加连续编号(这应该适应 n_row
和 n_col
).最终输出应该是这样的:
这是我当前的代码:
n_row <- 4
n_col <- 4
# Plot empty playing field
par(xaxs = "i", yaxs = "i")
plot.new()
plot.window(xlim = c(0.5, 4.5), ylim = c(0.5, 4.5))
grid(nx = n_col, ny = n_row, col = "grey")
box(lwd = 2)
您可以使用 mtext
at=
seq
从 par()$usr
派生的动态 n_row, n_col
。
n_row <- 6
n_col <- 7
# Plot empty playing field (4x4)
par(xaxs = "i", yaxs = "i")
plot.new()
plot.window(xlim = c(0.5, 4.5), ylim = c(0.5, 4.5))
grid(nx = n_col, ny = n_row, col = "grey")
box(lwd = 2)
xs <- do.call(seq, c(as.list(par()$usr[1:2]), length.out=n_col*2+1))
xs <- xs[seq_along(xs) %% 2 == 0]
mtext(seq_len(n_col), at=xs, side=1, line=1, cex=1.5)
ys <- do.call(seq, c(as.list(par()$usr[1:2]), length.out=n_row*2+1))
ys <- ys[seq_along(ys) %% 2 == 0]
mtext(seq_len(n_row), at=ys, side=2, line=1, cex=1.5, las=2)
另一种解决方案,具有axis
功能
axis(side=1, at=1:4, labels=1:4, cex.axis=2, tick=FALSE)
axis(side=2, at=1:4, labels=1:4, cex.axis=2, las=2, tick=FALSE)
我正在使用 R 控制台创建一个小游戏,我正在尝试向我的空白游戏场添加连续编号(这应该适应 n_row
和 n_col
).最终输出应该是这样的:
这是我当前的代码:
n_row <- 4
n_col <- 4
# Plot empty playing field
par(xaxs = "i", yaxs = "i")
plot.new()
plot.window(xlim = c(0.5, 4.5), ylim = c(0.5, 4.5))
grid(nx = n_col, ny = n_row, col = "grey")
box(lwd = 2)
您可以使用 mtext
at=
seq
从 par()$usr
派生的动态 n_row, n_col
。
n_row <- 6
n_col <- 7
# Plot empty playing field (4x4)
par(xaxs = "i", yaxs = "i")
plot.new()
plot.window(xlim = c(0.5, 4.5), ylim = c(0.5, 4.5))
grid(nx = n_col, ny = n_row, col = "grey")
box(lwd = 2)
xs <- do.call(seq, c(as.list(par()$usr[1:2]), length.out=n_col*2+1))
xs <- xs[seq_along(xs) %% 2 == 0]
mtext(seq_len(n_col), at=xs, side=1, line=1, cex=1.5)
ys <- do.call(seq, c(as.list(par()$usr[1:2]), length.out=n_row*2+1))
ys <- ys[seq_along(ys) %% 2 == 0]
mtext(seq_len(n_row), at=ys, side=2, line=1, cex=1.5, las=2)
另一种解决方案,具有axis
功能
axis(side=1, at=1:4, labels=1:4, cex.axis=2, tick=FALSE)
axis(side=2, at=1:4, labels=1:4, cex.axis=2, las=2, tick=FALSE)