如何更改绘图的背景颜色,仅针对特定区域?例如,从x = 2到x = 4?
额外问题:x和y坐标的组合是否也可能?(例如从(1,2)到(3,4))?
非常感谢!
Rei*_*son 18
这可以通过考虑与您的描述略有不同的情节来实现.基本上,您希望在x轴上的所需位置之间绘制一个彩色矩形,填充整个y轴限制范围.这可以使用rect(),并注意如何在下面的例子中,我抓住usr当前绘图的user()坐标给出我对y轴的限制,并且我们绘制超出这些限制以确保覆盖整个范围在情节中.
plot(1:10, 1:10, type = "n", axes = FALSE) ## no axes
lim <- par("usr")
rect(2, lim[3]-1, 4, lim[4]+1, border = "red", col = "red")
axis(1) ## add axes back
axis(2)
box() ## and the plot frame
Run Code Online (Sandbox Code Playgroud)
rect() 如果我们提供一个坐标向量,它可以绘制一系列矩形,它可以轻松处理你的奖金的任意x,y坐标的情况,但对于后者,如果你从X的向量开始,更容易避免错误坐标和另一个Y坐标如下:
X <- c(1,3)
Y <- c(2,4)
plot(1:10, 1:10, type = "n", axes = FALSE) ## no axes
lim <- par("usr")
rect(X[1], Y[1], X[2], Y[2], border = "red", col = "red")
axis(1) ## add axes back
axis(2)
box() ## and the plot frame
Run Code Online (Sandbox Code Playgroud)
您可以轻松获得奖金中的数据:
botleft <- c(1,2)
topright <- c(3,4)
plot(1:10, 1:10, type = "n", axes = FALSE) ## no axes
lim <- par("usr")
rect(botleft[1], botleft[2], topright[1], topright[2], border = "red",
col = "red")
axis(1) ## add axes back
axis(2)
box() ## and the plot frame
Run Code Online (Sandbox Code Playgroud)