【问题标题】:R plot with axes in the center以轴为中心的 R 图
【发布时间】:2015-06-05 04:05:51
【问题描述】:

默认情况下,R 中的笛卡尔坐标轴位于绘图的底部和左侧。
如何使轴居中,如下图所示?

例子

## 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')

【问题讨论】:

  • R 不喜欢制作这样的情节。您可以使用plot(..., axes=FALSE) 关闭所有轴,然后您可以使用lines() 或segments() 在任何您想要的地方绘制自己的线条,或者使用lwd= 使它们更粗。如果您实际上提供了一个 reproducible example 并提供了一些可以使用的数据以及您打算使用的绘图包(如果不是 base R),那么回答会更容易。
  • 实际上,如果您真的想要箭头,您可以使用axis(..., pos = 0) 轻松完成此操作,那么其他答案可以为您提供plot(x, y, xlim = c(-1.5, 1.5), ylim = c(-2, 2), axes = FALSE, ann = FALSE, type = 'n'); axis(1L, pos = 0, lwd.ticks = 0, labels = FALSE); axis(2L, pos = 0, lwd.ticks = 0, labels = FALSE); lines(x, y, col = 4L)

标签: r plot axis


【解决方案1】:

@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

一般来说,您可以使用基本图形绘制几乎任何东西,但它通常比使用更复杂的包更复杂,因为您必须手动绘制所有东西。

【讨论】:

  • 呵呵,感谢您努力从字面上解释问题的数字。最令人印象深刻。 :)
  • 感谢您为重现该行所做的额外努力。
【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多