简体   繁体   English

调整R图中的网格

[英]Adjust grid in R plot

I'm ploting a vector of 29 values in a plot using: 我正在使用以下方法在绘图中绘制29个值的向量:

plot(0:29, v, type="o", main="Title of the plot")

I try to adjust the grid to be unit by unit using: 我尝试使用以下方法将网格调整为单位:

grid(31, lw=2)

The problem is that x axis first value (0 position) doesn't start at the begining of the graph and grid starts at the begining, so both elements aren't aligned. 问题在于x轴第一个值(0位置)不是从图形的开头开始,而网格是从图形的开头开始,因此两个元素都不对齐。

How can solve this issue? 如何解决这个问题?

Use abline instead. 请改用abline

plot(0:10)
abline(h = 0:10)
abline(v = 0:10)

You can also force the exact x and y limits: 您还可以强制使用确切的x和y限制:

plot(0:10, 0:10, xlim = c(0,10), ylim = c(0, 10), xaxs = "i", yaxs = "i")
grid(10)

In general, using abline is easier and more robust. 通常,使用abline更容易且更可靠。 I also often plot with type = "n" , add lines/whatever in the background, then add the points. 我还经常使用type = "n"绘制,在背景中添加线条/任何内容,然后添加点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM