R 图中的网格

发布于 2024-08-16 06:27:47 字数 29 浏览 3 评论 0 原文

是否有命令可以轻松地将网格添加到 R 图上?

Is there a command to easily add a grid onto an R plot?

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

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

发布评论

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

评论(5

恋竹姑娘 2024-08-23 06:27:47

grid 命令似乎可以在任何需要的地方绘制网格线。我通常使用 abline 将线条精确地放置在我想要的位置。例如,

abline(v=(seq(0,100,25)), col="lightgray", lty="dotted")
abline(h=(seq(0,100,25)), col="lightgray", lty="dotted")

祝你好运!

The grid command seems to draw grid lines where-ever it feels like. I usually use abline to put lines exactly where I want them. For example,

abline(v=(seq(0,100,25)), col="lightgray", lty="dotted")
abline(h=(seq(0,100,25)), col="lightgray", lty="dotted")

Good luck!

节枝 2024-08-23 06:27:47

请参阅适用于标准图形的 help(grid) - 简短示例:

R> set.seed(42)
R> plot(cumsum(rnorm(100)), type='l')
R> grid()

lattice 有一个函数 panel.grid()您可以在自定义面板功能中使用。

顺便说一句,有一些帮助搜索功能,例如 help.search("something") 并且有一个名为 sos 使 R 网络搜索更加富有成效。

See help(grid) which works with standard graphics -- short example:

R> set.seed(42)
R> plot(cumsum(rnorm(100)), type='l')
R> grid()

The ggplot2 package defaults to showing grids due to its 'Grammar of Graphics' philosophy. And lattice has a function panel.grid() you can use in custom panel functions.

By the way, there are search functions for help as e.g. help.search("something") and there is an entire package called sos to make R web searches more fruitful.

活泼老夫 2024-08-23 06:27:47

如果您不使用自定义刻度间隔,则可以直接通过 plot() 命令控制网格和坐标区参数:

plot(cumsum(rnorm(100)), type='l', panel.first=grid())

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

If you are not using a custom tick interval, you can control the grid and axes parameters directly from the plot() command:

plot(cumsum(rnorm(100)), type='l', panel.first=grid())

The plot.default() documentation provides more information about these parameters.

陌路黄昏 2024-08-23 06:27:47

我同意cbare的观点。
使用 abline 仅在您真正需要的地方绘制线条。

我上一个代码的示例:

abline(v=c(39448, 39814), col="grey40")
abline(h=c(-0.6, -0.4, -0.2, 0.2,0.4,0.6), col="grey10", lty="dotted") 

请记住:

v 用于垂直线。
h 表示水平。

利用命令

lty 获取虚线
color 为浅色线

,以获得“无重网格”。

I agree with cbare.
Use abline to draw lines only where you really need.

Example from my last code:

abline(v=c(39448, 39814), col="grey40")
abline(h=c(-0.6, -0.4, -0.2, 0.2,0.4,0.6), col="grey10", lty="dotted") 

remember that:

v is for vertical lines.
h for horizontal.

exploit the commands

lty for dotted line
color for light coloured line

in order to obtain "no heavy grid".

无人问我粥可暖 2024-08-23 06:27:47

另一种选择是对垂直和水平网格线使用 axis 函数:

x <- rnorm(100)
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Horizontal grid  
axis(2, tck = 1, lty = 2, col = "gray")

# Only vertical grid
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")

# Only horizontal grid
plot(x)
# Horizontal grid  
axis(2, tck = 1, lty = 2, col = "gray")

创建于 2022 年 8 月 20 日,使用 reprex v2.0.2

您可以使用 at 参数指定网格线的位置。

Another option is using the axis function for vertical and horizontal grid lines:

x <- rnorm(100)
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Horizontal grid  
axis(2, tck = 1, lty = 2, col = "gray")

# Only vertical grid
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")

# Only horizontal grid
plot(x)
# Horizontal grid  
axis(2, tck = 1, lty = 2, col = "gray")

Created on 2022-08-20 with reprex v2.0.2

You can specify the position of the grid lines using the at argument.

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