网格线的限制长度(R图) [英] Limit length of gridline (R plot)

查看:82
本文介绍了网格线的限制长度(R图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 plot 使 R 图中的网格线不超出图的边界?即需要定义网格线的起点和终点.

How can I make the gridlines in an R plot using plot to not extend beyond the plot's boundaries? i.e. need to define gridlines' origin and end.

在给出的例子中,我希望 grid 绘制的网格线和 abline 绘制的 y=7.5 的网格线从 y 轴延伸到右边缘的盒子.

On the example given, I want the gridlines drawn by grid and the gridline at y=7.5 drawn by abline to extend from the y-axis to the right edge of the box.

plot(1:10,1:10,type = "n")
abline(h = 7.5, v = 0, col = "gray60", lty = 3)
grid(10,10)

推荐答案

您可能(在不知不觉中)设置了 xpd = TRUExpd = NA.如果您在绘图调用之前设置 xpd = FALSE,这将阻止您的 gridabline(或 Base R 中的任何绘图添加对象) 超出绘图区域.

You might have (unknowingly) set xpd = TRUE or xpd = NA. If you set your xpd = FALSE before your plot call, this will prevent your grid or abline (or ANY plot-additive object in Base R) from going beyond the plotting region.

因此,在您的 plot() 调用之前执行以下操作:

So, do the following before your plot() call:

par(xpd = FALSE)

对于您未来的绘图案例,请记住 xpd = FALSE 可以本地在命令中使用,例如 abline(h = 2, xpd = FALSE) 或者它可以全局使用,就像我在你的情况下使用它一样.

For your future plotting cases remember that xpd = FALSE can be both locally used within a command, like abline(h = 2, xpd = FALSE) or it can be used globally, like how I used it for your case.

进一步记住 xpd = NAxpd = FALSE 不同.前者允许绘图远远超出绘图区域到 par("oma")(外边距区域),而后者将所有内容保留在 par("usr")(内部绘图)区域.这些特性有助于 R 中的各种可视化.

Further remember that xpd = NA IS NOT the same as xpd = FALSE. The former allows plotting to go well beyond the plotting region to par("oma") (outer margin area), while the latter keeps everything within the par("usr") (inner plotting) region. These features are helpful in various visualizations in R.

这篇关于网格线的限制长度(R图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆