本文介绍了如何将x和y轴的截距放在(0,0)处并将x和y轴延伸到图的边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要绘制x^2.我可以按以下方式使用curve().

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

curve(x^2, -5, 5)

但是,我希望轴经过(0,0).我可以做以下事情:

However, I would like the axes to go through (0, 0). I could do something as follows:

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

最后我得到类似下面的内容,看起来不错.但是我唯一的抱怨是,这种绘制轴的方式使实际轴(例如x轴的-4和4之间的线段)比右侧和左侧的线段更粗. y轴也是如此.我想知道是否有更好的方法来绘制轴.谢谢!

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!

推荐答案

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

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 :

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

哪个给:

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

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 :

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

哪个给:

这篇关于如何将x和y轴的截距放在(0,0)处并将x和y轴延伸到图的边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:42