网格线与轴上的刻度一致
我很尴尬地问这个简单的问题,但每当我创建一个绘图时,我已经好几天都在思考:
plot (x = 1:10, y = rnorm (10, 5, 2))
grid (10,10, lty = 6, col = "cornsilk2")
我想将网格放置在轴标记的位置,即在 2, 4, 6, 8, 10 处x 轴,类似地 y 轴为 3、4、5、6、7、8。
我想自动化该过程,因为只要绘图大小发生变化,默认标签行为就会发生变化。请参阅以下图:
I am embarrassed to ask this simple question, but has been in kicking my mind for several days whenever I create a plot:
plot (x = 1:10, y = rnorm (10, 5, 2))
grid (10,10, lty = 6, col = "cornsilk2")
I want to position the grids right at where axis are labelled, i.e. at 2, 4, 6, 8, 10 in x axis and similarly 3, 4, 5, 6, 7, 8 in y axis.
I want to automate the process as whenever the plot size changes the default label behaviour changes. See the following plot:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于后代,这是手动执行此操作的冗长方法:
For posterity, here is the long-winded way of doing it manually:
此处提供的答案要简单得多,尽管您可能不喜欢缺少“免费”轴两端的空间”。简而言之,
The answer provided here is much more straightforward, although you may dislike the lack of "free space" at each end of the axes. In brief,
来自
nx
和ny
参数的?grid
描述:From
?grid
description of thenx
andny
arguments:作为参考,如果我们没有定义自定义刻度间隔,有一种方法可以直接从plot()命令控制网格和坐标轴参数:
plot.default()文档提供了有关这些参数的更多信息。
使用自定义刻度间隔时,最简单的方法是使用 abline 绘制网格:
有关自定义刻度间隔的更多信息,请参阅此主题和此处用于网格对齐。
For reference, there is a way to control the grid and axes parameters directly from the plot() command, if we are not defining a custom tick interval:
The plot.default() documentation gives more information about these parameters.
When using a custom ticks interval, the easiest is to draw the grid using abline:
More information about custom tick intervals in this thread and here for grid alignment.