2011-09-06 4 views
6

플롯별로 계산할 값이 xlimylim 인 수많은 플롯이 생성됩니다. 전설 영역을 (실제 플롯 주위의 상자 바로 위) 플롯 영역 외부에두고 싶습니다. 그러나 플롯 영역 주위에서 상자의 최대 y 값을 얻는 방법을 알아낼 수 없습니다.플롯() 경계 상자 값 가져 오기

이렇게하는 방법이 있습니까? legend() x 및 y 값을 수동으로 변경하여 원하는 위치에서 범례를 이동할 수는 있지만 작성하는 그래프의 양은 오래 걸립니다.

감사합니다. par() 제어 경계와 플롯의 마진

-JM

+1

'? '와'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

OMA의, OMD, 오미 인수 - 그들은 par()$omd (등)를 사용하여 조회 할 수 있습니다. par(oma=c()) (벡터는 최대 4 개의 값을 가질 수 있습니다 -? par 참조)을 사용하여 설정합니다.

+0

'par ("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)