2015-06-05 61 views
1

默認情況下,R中的笛卡爾座標軸位於座標圖的底部和左側。
如何使軸居中,如下圖所示?R座標軸位於中心

enter image description here

## using; data; generated; by; bgoldst;; 
## estimate curve 
x <- seq(-1,1.5,0.1); 
y <- c(1.3,1.32,1.33,1.32,1.25,1.1,0.7,0.5,0.4,0.38,0.4,0.41,0.42,0.43,0.44,0.4,0.3,0.1,0,-0.05,-0.1,-0.15,-0.2,-0.24,-0.28,-0.3); 
f <- splinefun(x,y); 

## calculate precise points along estimated curve 
x <- seq(-1,1.5,0.01); 
y <- f(x); 
plot(x, y, type = 'l') 

enter image description here

+2

R不喜歡製作這樣的情節。你可以用'plot(...,axes = FALSE)關掉所有的軸,然後你可以用'lines()'或者'segments()'來繪製你自己的行,或許使用' LWD ='。如果你真的提供了一個[可重現的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)和一些數據一起玩,它會更容易回答和你打算使用的繪圖軟件包,如果不是基於R. – MrFlick

+0

實際上你可以用'axis(...,pos = 0)'輕鬆地做到這一點,如果你真的想要箭頭,那麼其他答案可以給你那個'plot (x,y,xlim = c(-1.5,1.5),ylim = c(-2,2),axes = FALSE,ann = FALSE,type ='n');軸(1L,pos = 0,lwd.ticks = 0,labels = FALSE);軸(2L,pos = 0,lwd.ticks = 0,labels = FALSE);行(x,y,col = 4L)' – rawr

回答

2

我覺得像下面這樣做你會在基礎圖形喜歡什麼:

## Simulate your data: 
x <- seq(-3, 3, by=0.01) 
y <- 0.5*x - 0.3*x^2 + 0.4*x^3 

## Plot the polynomial function, removing axis ticks and bounding box, 
## as well as the axis labels: 
plot(x, y, 
    type="l", 
    xaxt='n', yaxt='n', 
    bty='n', 
    xlab='', ylab='', 
    col="blue") 

## Next add in your axis arrows: 
arrows(min(x), 0, max(x), 0, lwd=1, length=0.15) 
arrows(0, min(y), 0, max(y), lwd=1, length=0.15) 

## And plot your x/y labels. Note that if you want them 
## actually at the end of the arrows you would need to 
## remove the pos= argument and shorten your arrows by 
## a small amount. To match your original figure, you can 
## alter the x/y coordinate to be the max() instead. 
text(0, min(y), "y", pos=2) 
text(min(x), 0, "x", pos=3) 

Plot

+0

加快+1 – bgoldst

3

@ForrestRStevens對我來說太快了,我太忙了嘗試使用花:)

## estimate curve 
x <- seq(-1,1.5,0.1); 
y <- c(1.3,1.32,1.33,1.32,1.25,1.1,0.7,0.5,0.4,0.38,0.4,0.41,0.42,0.43,0.44,0.4,0.3,0.1,0,-0.05,-0.1,-0.15,-0.2,-0.24,-0.28,-0.3); 
f <- splinefun(x,y); 

## calculate precise points along estimated curve 
x <- seq(-1,1.5,0.01); 
y <- f(x); 

## precompute limits 
xlim <- c(min(x),max(x)); 
ylim <- c(min(y)-0.4,max(y)+0.2); 

## set global plot params 
par(xaxs='i',yaxs='i',mar=c(1,1,3,3)+0.1); ## "internal" axis spacing, meaning no extended range, and slightly adjust margins 

## draw plot 
plot(NA,xlim=xlim,ylim=ylim,axes=F,ann=F); ## set plot bounds, no default ornaments 
arrows(c(0,xlim[1]),c(ylim[1],0),c(0,xlim[2]),c(ylim[2],0),0.05); ## draw custom axes 
mtext('y',3,1,at=0,las=1,cex=0.8,family='serif'); ## y label 
mtext('x',4,1,at=0,las=1,cex=0.8,family='serif'); ## x label 
lines(x,y,col='#aaaacc'); ## draw line on top 

plot

一般估計你的曲線,你可以用圖形基地繪製幾乎任何東西,但是與使用更復雜的軟件包相比,這通常更爲複雜,因爲您必須手工繪製所有內容。

+0

heh,+ 1爲您努力解釋問題的數字。印象最深刻。 :) –

+0

感謝您爲重現該行額外的努力。 –