2015-06-05 62 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

感谢您为重现该行额外的努力。 –