对齐grid()以绘制刻度线 [英] Align grid() to plot ticks

查看:147
本文介绍了对齐grid()以绘制刻度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在图上添加刻度(比默认刻度更多)时,如何获得grid()以使网格与刻度对齐?

When adding ticks to a plot (more ticks than default), how does one get the grid() to align the grid to the ticks?

plot(1:10,las=1,xaxp  = c(0, 10, 10),xlim=c(0,10), ylim=c(0,10))
grid(lwd=2, nx=10, ny=10)

试图更改xlim和网格中nx arg的不同数字(单元数),但网格根本无法对齐.

Tried changed the xlim and different numbers for the nx arg in grid (number of cells), but the grid simply doesn't line up.

相关,但未回答问题: 相关,并使用解决方法: 将网格与刻度线对齐

解决方法是最有效的选择吗?

Is the workaround the most efficient option?

推荐答案

您可以使用abline绘制网格.您可以使用h(对于水平线)和v(对于垂直线)指定网格应位于的位置

You could use abline to draw grids. You can specify where the grids should be with h (for horizontal lines) and v (for vertical lines)

#Plot
plot(1:10,las=1,xaxp  = c(0, 10, 10),xlim=c(0,10), ylim=c(0,10))
#Add horizontal grid  
abline(h = c(0,2,4,6,8,10), lty = 2, col = "grey")
#Add vertical grid
abline(v = 1:10,  lty = 2, col = "grey")

另一种解决方法是使用axis,其中tck的值为1.使用axis,您可以使用at

Another workaround is to use axis where tck value is 1. With axis, you can specify where the grids should be with at

#Plot
plot(1:10,las=1,xaxp  = c(0, 10, 10),xlim=c(0,10), ylim=c(0,10))

#Add horizontal grid  
axis(2, at = c(0,2,4,6,8,10), tck = 1, lty = 2, col = "grey", labels = NA)

#Add vertical grid
axis(1, at = 1:10, tck = 1, lty = 2, col = "grey", labels = NA)

#Add box around plot
box()

这篇关于对齐grid()以绘制刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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