Reputation: 25
I'm currently struggling with finding out, how to spread the axis labels evenly.
Here is the code I'm using:
plot(1:10, type = "p", xlab = "", ylab = "")
axis(1, at = c(1:10))
axis(2, at = c(1:10))
grid(nx = 10, ny = 10)
What I'm trying to achieve is to have the data points in the center of the respective grid box and to have the labels (1-10) to be in the center. How can I achieve this?
Any help or direction would be greatly appreciated, thank you!
Upvotes: 0
Views: 160
Reputation: 174586
As the help file for grid
tells you, when you need more control of the grid lines, you should draw them directly with abline
. When you want precise control of the axis limits rather than allowing R to choose pretty limits, you specify xaxs = "i"
and yaxs = "i"
in your initial plot command.
plot(1:10, type = "p", xlab = "", ylab = "",
xlim = c(0.5, 10.5), ylim = c(0.5, 10.5), yaxs = "i", xaxs = "i")
abline(h = 2:10 - 0.5, v = 2:10 - 0.5, lty = "dotted", col = "lightgray")
axis(1, at = c(0:11))
axis(2, at = c(1:10))
Upvotes: 2
Reputation: 49670
I agree with Allan Cameron that when you want that much control it is best to switch tools to something like abline
. But if you really want to use grid, you can do so by explicitly setting the limits of your plot:
plot(1:10, xlim=c(0.5,10.5), ylim=c(0.5,10.5), xaxs='i', yaxs='i')
grid(nx=10, ny=10)
You may want to do this even if using abline
since it will make the corner and edge boxes be the same size as the others (and center the points in those boxes).
Upvotes: 2