简体   繁体   中英

Adjust grid in R plot

I'm ploting a vector of 29 values in a plot using:

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.

How can solve this issue?

Use abline instead.

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

You can also force the exact x and y limits:

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. I also often plot with type = "n" , add lines/whatever in the background, then add the points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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