1

How can I align the grid in the image with the data limits of a plot? My code places the grid in an undesirable fashion. I do not want to use abline as grid seems more professional.

plot(NA, xlim=c(0,1.0), ylim=c(0,1.0), xlab="X", ylab="Y", lwd=3)
grid(nx = 10, ny = 10, col = "lightgray", lty = "dotted", lwd = par("lwd"))
antetokounmpo
  • 167
  • 1
  • 7
  • This question was answered [HERE](https://stackoverflow.com/a/42011679/4752675). The accepted answer uses `abline` but there is another answer that does not use `abline` – G5W Dec 15 '18 at 11:24
  • Appreciate your redirection. That answer does not arrange the grid, but the plot. As a result abline provided below looks better. Thanks! – antetokounmpo Dec 15 '18 at 12:02

1 Answers1

0

Grid seems to have other purposes so I could not find a way, but it is very simple with abline. In case you change your mind about abline, here is the way:

plot(NA, xlim=c(0,1.0), ylim=c(0,1.0), xlab="X", ylab="Y", lwd=3)
#Add horizontal grid  
abline(h = c(seq(from=0, to=1.0, by=0.1)), lty = "dotted", col = "grey")
#Add vertical grid
abline(v = c(seq(from=0, to=1.0, by=0.1)), lty = "dotted", col = "grey")
Tolga
  • 116
  • 2
  • 12