2012-01-05 86 views

回答

15

這可以通過思考的情節有所不同,以你的描述來實現。基本上,您想要在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()可以繪製矩形的序列,如果我們提供座標的載體,它可以輕鬆地處理爲任意的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 

你可以很容易地擁有的數據,你的獎金有它:

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 
+3

你也可以使用'多邊形'來遮蔽更復雜的區域。 – 2012-01-05 12:47:45

+3

至少有一些'plot'方法,你也可以將'rect'調用包裝在'panel.first'中。 – baptiste 2012-01-05 20:10:18

+0

好點! – 2012-01-05 20:27:56