2011-09-06 83 views
6

我產生了許多情節與xlimylim值我正在計算在每個情節的基礎。我希望把我的傳說繪圖區(略高於周圍的主線劇情框)外,但我無法弄清楚如何繞過我的繪圖區框的最大y值。獲取地塊()邊界框的值

是否有連這樣的方法?能打動我,我通過手動改變legend() x和y的值想要的傳說,但這需要很長的時間圖我創建的數量。

謝謝!

-JM

+1

試着看''par'和'usr'參數。 – joran

+0

你是否在使用'ggplot'作爲你的情節?如果您向我們展示一些示例代碼和圖形,它可能有助於理解您想要的內容。 – Andrie

回答

10

下面是一個基本示例,說明我使用?legend的代碼示例之一尋找的內容。

#Construct some data and start the plot 
x <- 0:64/64 
y <- sin(3*pi*x) 
plot(x, y, type="l", col="blue") 
points(x, y, pch=21, bg="white") 

#Grab the plotting region dimensions 
rng <- par("usr") 

#Call your legend with plot = FALSE to get its dimensions 
lg <- legend(rng[1],rng[2], "sin(c x)", pch=21, 
      pt.bg="white", lty=1, col = "blue",plot = FALSE) 

#Once you have the dimensions in lg, use them to adjust 
# the legend position 
#Note the use of xpd = NA to allow plotting outside plotting region    
legend(rng[1],rng[4] + lg$rect$h, "sin(c x)", pch=21, 
      pt.bg="white", lty=1, col = "blue",plot = TRUE, xpd = NA) 

enter image description here

+0

謝謝!這正是我需要的! –

1

par()控制界限和情節的邊距OMA,OMD,和OMI參數 - 它們可使用查詢par()$omd(等)。並設置(如果需要)使用par(oma=c())(其中矢量最多可以有4個值 - 請參閱?par)

+0

'面值(「OMD」)'會檢索命名參數更標準的方式。 –

3

命令par('usr')將返回邊框的座標,但你也可以使用grconvertXgrconvertY功能。一個簡單的例子:

plot(1:10) 
par(xpd=NA) 
legend(par('usr')[1], par('usr')[4], yjust=0, legend='anything', pch=1) 
legend(grconvertX(1, from='npc'), grconvertY(1, from='npc'), yjust=0, 
xjust=1, legend='something', lty=1)