如何将x和y轴的截距放在(0,0)处并将x和y轴延伸到绘图的边缘

时间:2023-02-02 00:02:28

Suppose I want to plot x^2. I can use curve() as follows.

假设我想绘制x ^ 2。我可以使用curve()如下。

curve(x^2, -5, 5)

如何将x和y轴的截距放在(0,0)处并将x和y轴延伸到绘图的边缘 However, I would like the axes to go through (0, 0). I could do something as follows:

但是,我希望轴能够通过(0,0)。我可以做如下的事情:

curve(x^2, -5, 5, axes=FALSE)
axis(1, pos=0)
axis(2, pos=0)
abline(h=0)
abline(v=0)

And I end up getting something like below, which looks OK. But the only gripe I have is that this way of plotting axes makes the actual axes - for example the segment between -4 and 4 of the x-axis - thicker than the segments to the right side and the left side. The same goes with the y axis. I wonder if there is a better way of plotting the axes. Thank you!

我最终得到了类似下面的东西,看起来还不错。但我唯一的抱怨是这种绘制轴的方式使得实际轴 - 例如x轴的-4到4之间的段 - 比右侧和左侧的段厚。 y轴也是如此。我想知道是否有更好的绘制轴的方法。谢谢!

如何将x和y轴的截距放在(0,0)处并将x和y轴延伸到绘图的边缘

2 个解决方案

#1


15  

By default, axis() computes automatically the tick marks position, but you can define them manually with the at argument. So a workaround could be something like :

默认情况下,axis()自动计算刻度标记位置,但您可以使用at参数手动定义它们。因此,解决方法可能是这样的:

curve(x^2, -5, 5, axes=FALSE)
axis(1, pos=0, at=-5:5)
axis(2, pos=0)

Which gives :

这使 :

如何将x和y轴的截距放在(0,0)处并将x和y轴延伸到绘图的边缘

The problem is that you have to manually determine the position of each tick mark. A slightly better solution would be to compute them with the axTicks function (the one used by default) but calling this one with a custom axp argument which allows you to specify respectively the minimum, maximum and number of intervals for the ticks in the axis :

问题是你必须手动确定每个刻度线的位置。稍微好一点的解决方案是使用axTicks函数(默认情况下使用的函数)计算它们,但使用自定义axp参数调用此函数,该参数允许您分别指定轴中刻度线的最小值,最大值和间隔数:

curve(x^2, -5, 5, axes=FALSE)
axis(1, pos=0, at=axTicks(1,axp=c(-10,10,10)))
axis(2, pos=0)

Which gives :

这使 :

如何将x和y轴的截距放在(0,0)处并将x和y轴延伸到绘图的边缘

#2


2  

The arguments yaxs and xaxs control spacing around plots. Set to "i" to omit this:

参数yaxs和xaxs控制图周围的间距。设置为“i”以省略此:

curve(x^2, -5, 5, yaxs = "i")

See also: https://*.com/a/12300673/567015

另见:https://*.com/a/12300673/567015

#1


15  

By default, axis() computes automatically the tick marks position, but you can define them manually with the at argument. So a workaround could be something like :

默认情况下,axis()自动计算刻度标记位置,但您可以使用at参数手动定义它们。因此,解决方法可能是这样的:

curve(x^2, -5, 5, axes=FALSE)
axis(1, pos=0, at=-5:5)
axis(2, pos=0)

Which gives :

这使 :

如何将x和y轴的截距放在(0,0)处并将x和y轴延伸到绘图的边缘

The problem is that you have to manually determine the position of each tick mark. A slightly better solution would be to compute them with the axTicks function (the one used by default) but calling this one with a custom axp argument which allows you to specify respectively the minimum, maximum and number of intervals for the ticks in the axis :

问题是你必须手动确定每个刻度线的位置。稍微好一点的解决方案是使用axTicks函数(默认情况下使用的函数)计算它们,但使用自定义axp参数调用此函数,该参数允许您分别指定轴中刻度线的最小值,最大值和间隔数:

curve(x^2, -5, 5, axes=FALSE)
axis(1, pos=0, at=axTicks(1,axp=c(-10,10,10)))
axis(2, pos=0)

Which gives :

这使 :

如何将x和y轴的截距放在(0,0)处并将x和y轴延伸到绘图的边缘

#2


2  

The arguments yaxs and xaxs control spacing around plots. Set to "i" to omit this:

参数yaxs和xaxs控制图周围的间距。设置为“i”以省略此:

curve(x^2, -5, 5, yaxs = "i")

See also: https://*.com/a/12300673/567015

另见:https://*.com/a/12300673/567015