如何用基数R绘制一个简单的矩形

时间:2021-05-16 15:20:54

标签: r graphics rectangles

我想用只使用 R 基础绘制一个矩形:

矩形:宽 = 2.5 厘米,高 = 1 厘米

所需的输出:

enter image description here

原则上 drawBox 可以做到这一点,但我正在寻找基本的 R 解决方案:

library(draw)
drawBox(x =1.5, y = 4, width = 2.5, height = 1)

2 个答案:

答案 0 :(得分:2)

您可以先创建一个空图,然后使用基础 R 函数 rect()

plot(NULL, axes = FALSE, xlab = "", ylab = "", xlim = c(0, 2.5), ylim = c(0, 1))
rect(xleft = 0, xright = 2.5, ybottom = 0, ytop = 1)

enter image description here

答案 1 :(得分:1)

以下情况如何:

par(mar = rep(0.2,4))
plot(NA, xlim = c(0,1), ylim = c(0,1), axes = FALSE, xlab = "", ylab = "")
box()

然后将其嵌入到具有指定大小的 pdf 中。

相关问题