网格线与轴上的刻度一致

发布于 2024-12-14 18:49:33 字数 406 浏览 3 评论 0原文

我很尴尬地问这个简单的问题,但每当我创建一个绘图时,我已经好几天都在思考:

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.

enter image description here

I want to automate the process as whenever the plot size changes the default label behaviour changes. See the following plot:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瑕疵 2024-12-21 18:49:34

对于后代,这是手动执行此操作的冗长方法:

plot (x = 1:10, y = rnorm (10, 5, 2))
grid (lty = 6, col = "cornsilk2")

xaxp <- par("xaxp")
yaxp <- par("yaxp")

abline(v=seq(xaxp[1], xaxp[2], (xaxp[2]-xaxp[1])/xaxp[3]), lty=6, col = "cornsilk2")
abline(h=seq(yaxp[1], yaxp[2], (yaxp[2]-yaxp[1])/yaxp[3]), lty=6, col = "cornsilk2")

For posterity, here is the long-winded way of doing it manually:

plot (x = 1:10, y = rnorm (10, 5, 2))
grid (lty = 6, col = "cornsilk2")

xaxp <- par("xaxp")
yaxp <- par("yaxp")

abline(v=seq(xaxp[1], xaxp[2], (xaxp[2]-xaxp[1])/xaxp[3]), lty=6, col = "cornsilk2")
abline(h=seq(yaxp[1], yaxp[2], (yaxp[2]-yaxp[1])/yaxp[3]), lty=6, col = "cornsilk2")
云醉月微眠 2024-12-21 18:49:34

此处提供的答案要简单得多,尽管您可能不喜欢缺少“免费”轴两端的空间”。简而言之,

问题是网格将 nx 网格线放入用户空间,
但绘图每边都增加了 4% 的额外空间。你可以掌控一切
的这个。将 xaxs="i", yaxs="i" 添加到绘图中将关闭
额外的空间。但是这样你的右上角就会被切断,所以你
需要更改 xlim 和 ylim 值并更改 nx 以匹配

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,

The problem is that grid is putting nx grid lines in the user space,
but plot is adding 4% extra space on each side. You can take control
of this. Adding xaxs="i", yaxs="i" to your plot will turn off the
extra space. But then your upper right point will be cut off, so you
need to change the xlim and ylim values and change nx to match

-柠檬树下少年和吉他 2024-12-21 18:49:33

来自 nxny 参数的 ?grid 描述:

当为 NULL 时,默认情况下,网格与刻度线对齐
相应的默认轴(即由 axTicks 计算的刻度线)

plot (x = 1:10, y = rnorm (10, 5, 2)) 
grid (NULL,NULL, lty = 6, col = "cornsilk2") 

From ?grid description of the nx and ny arguments:

When NULL, as per default, the grid aligns with the tick marks on the
corresponding default axis (i.e., tickmarks as computed by axTicks)

plot (x = 1:10, y = rnorm (10, 5, 2)) 
grid (NULL,NULL, lty = 6, col = "cornsilk2") 
默嘫て 2024-12-21 18:49:33

作为参考,如果我们没有定义自定义刻度间隔,有一种方法可以直接从plot()命令控制网格和坐标轴参数:

plot(x = 1:10, y = rnorm(10, 5, 2), xlim=c(1, 10), ylim=c(1, 10), panel.first=grid())

plot.default()文档提供了有关这些参数的更多信息。

使用自定义刻度间隔时,最简单的方法是使用 abline 绘制网格:

plot(x = 1:10, y = rnorm(10, 5, 2), xaxp=c(1, 10, 10), yaxp=c(1, 10, 10), axes=FALSE)
axis(1, 1:10)
axis(2, 1:10)
abline(h=1:10, v=1:10, col="gray", lty=3)

grid example

有关自定义刻度间隔的更多信息,请参阅此主题此处用于网格对齐。

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:

plot(x = 1:10, y = rnorm(10, 5, 2), xlim=c(1, 10), ylim=c(1, 10), panel.first=grid())

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:

plot(x = 1:10, y = rnorm(10, 5, 2), xaxp=c(1, 10, 10), yaxp=c(1, 10, 10), axes=FALSE)
axis(1, 1:10)
axis(2, 1:10)
abline(h=1:10, v=1:10, col="gray", lty=3)

grid example

More information about custom tick intervals in this thread and here for grid alignment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文