R:仅更改特定区域的绘图背景颜色(基于x值)

时间:2021-06-09 14:58:02

How do I change the background color for a plot, only for a specific area? For example, from x=2 to x=4?

如何更改绘图的背景颜色,仅针对特定区域?例如,从x = 2到x = 4?

Bonus question: is it also possible for a combination of x and y coordinates? (for example from (1,2) to (3,4))?

额外问题:x和y坐标的组合是否也可能? (例如从(1,2)到(3,4))?

Many thanks!

非常感谢!

1 个解决方案

#1


16  

This can be achieved by thinking about the plot somewhat differently to your description. Basically, you want to draw a coloured rectangle between the desired positions on the x-axis, filling the entire y-axis limit range. This can be achieved using rect(), and note how, in the example below, I grab the user (usr) coordinates of the current plot to give me the limits on the y-axis and that we draw beyond these limits to ensure the full range is covered in the plot.

这可以通过考虑与您的描述略有不同的情节来实现。基本上,您希望在x轴上的所需位置之间绘制一个彩色矩形,填充整个y轴限制范围。这可以使用rect()实现,并注意在下面的示例中,我如何获取当前绘图的用户(usr)坐标以给出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

rect() can draw a sequence of rectangles if we provide a vector of coordinates, and it can easily handle the case for the arbitrary x,y coordinates of your bonus, but for the latter it is easier to avoid mistakes if you start with a vector of X coordinates and another for the Y coordinates as below:

如果我们提供一个坐标向量,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

You could just as easily have the data as you have it in the bonus:

您可以轻松获得奖金中的数据:

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

#1


16  

This can be achieved by thinking about the plot somewhat differently to your description. Basically, you want to draw a coloured rectangle between the desired positions on the x-axis, filling the entire y-axis limit range. This can be achieved using rect(), and note how, in the example below, I grab the user (usr) coordinates of the current plot to give me the limits on the y-axis and that we draw beyond these limits to ensure the full range is covered in the plot.

这可以通过考虑与您的描述略有不同的情节来实现。基本上,您希望在x轴上的所需位置之间绘制一个彩色矩形,填充整个y轴限制范围。这可以使用rect()实现,并注意在下面的示例中,我如何获取当前绘图的用户(usr)坐标以给出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

rect() can draw a sequence of rectangles if we provide a vector of coordinates, and it can easily handle the case for the arbitrary x,y coordinates of your bonus, but for the latter it is easier to avoid mistakes if you start with a vector of X coordinates and another for the Y coordinates as below:

如果我们提供一个坐标向量,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

You could just as easily have the data as you have it in the bonus:

您可以轻松获得奖金中的数据:

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