2011-12-02 61 views
4

我使用下面的代碼:如何在R中繪製靜態軸值?

x <-sample(1:100, 10); 
y <-sample(1:100, 10); 
plot(x,y); 

,但我想創建與X靜態值,Y軸的曲線圖。 問題在於,有時x軸(例如)在圖上顯示的值範圍爲0到80,而其他值則從20到100,具體取決於x內的隨機值。

無論x和y矩陣內的值如何,在所有情況下,我都希望x,y在圖中的值範圍爲0到100。

有沒有辦法做到這一點?

在此先感謝!

回答

1

有兩個函數可以使用,內plot()您可以使用手動設置軸xlim()ylim()

+0

不知道,但也許你的意思是'xlim'和'ylim'是給'plot',或者更具體地說'plot.default'? – joran

5

根據你的意思是「數值範圍從1到100」,下列其中一個應該做的。

在第一個,plot默認延伸4%軸界限兩端

在第二個,xaxs="i"yaxs="i"用來抑制這種行爲。 (詳情請參見?par

plot(x,y, xlim=c(0,100), ylim=c(0,100)) 

enter image description here

plot(x,y, xlim=c(0,100), ylim=c(0,100), xaxs="i", yaxs="i") 

enter image description here

1

首先,你必須找出邊界 - 你可以選擇一些(如0,100),或者找到與range功能脫離真正的界限。然後您將使用xlimylim繪圖選項。這是range找到真正的邊界的一個例子:

x1 <-sample(1:100, 10); 
y1 <-sample(1:100, 10); 
x2 <-sample(1:100, 10); 
y2 <-sample(1:100, 10); 

par(mfrow=c(1, 2)) 
range_x <- range(x1, x2); // find the range that spans all the data, for x 
range_y <- range(y1, y2); // and for y 
plot(x1, y1, xlim = range_x, ylim = range_y); 
plot(x2, y2, xlim = range_x, ylim = range_y); 
+0

但是OP要求「在所有情況下,我都希望x,y在圖中的值範圍爲0到100」。這並沒有完成... –