2014-01-21 31 views
4

我知道我可以在繪圖時指定寬高比,例如, plot(x,y,asp=5)。在允許自動縮放後,有什麼方法可以檢索寬高比(如plot(x,y))?我問的原因是我在玩text(x,y,'mystring',srt=local_slope),我根據潛在曲線和x價值計算local_slope。問題是,對於asp!=1,這會使文本以與繪製的數據集的顯示斜率不同的角度旋轉。示例:base :: plot - 我可以檢索繪製的縱橫比嗎?

x<- -10:10 
y<- x^2 
plot(x,y,t='l',asp=0.1) 
# the slope at x=1 is 2 but the default plot aspect ratio is far from 1:1 
text(1,1,'foo',srt= 180/pi*atan(2)) #ugly-looking 
text(-1,1,'bar',srt= (180/pi*atan(2/10))) #better 

回答

7
x<- -10:10 
y<- x^2 
plot(x,y,t='l',asp=0.1) 
### the slope at x=1 is 2 but the default plot aspect ratio is far from 1:1 
text(1,1,'foo',srt= 180/pi*atan(2)) #ugly-looking 
text(-1,1,'bar',srt= (180/pi*atan(2/10))) #better 

獲取寬度和英寸繪圖區域的高度...

ff <- par("pin") 
ff[2]/ff[1] ## 1.00299 

現在手動調整的情節......

ff <- par("pin") 
ff[2]/ff[1] ## 0.38 

您還可以使用par("usr")以用戶單位整理寬高比 ,但我還沒有弄清楚正確的 一套比率...... MASS::eqscplot的膽量也許也是有啓發性的。

+0

謝謝。我可以生成'myasp <-ff [2]/ff [1] * diff(range(y))/ diff(range(x))''。 //我懷疑有幾個高級繪圖軟件可以處理這個問題;我只是還沒有那麼遠:-) –

+1

@CarlWitthoft - 'range(y)'和'range(x)'不一定給出y軸和x軸覆蓋的全部值。例如,當par(xaxs =「r」) - R的默認值時,它們不會。因此,執行相當於'myasp < - with(par(),(pin [2]/pin [1])/(diff(usr [3:4])/ diff(usr [1:2] )))' –

+0

@ JoshO'Brien謝謝,我會對我的計算更加謹慎。 –