Reputation: 171
I've encountered a problem with R's grid() function on fixed aspect-ratio plots.
When plotting with a fixed aspect-ratio, R's default behavior seems to be to resize the plot inside the plot window, rather than resizing the plot window itself, as seen here:
Aside from the unsightly empty space on either side of the plot (this is minor, I can deal with it), this poses a problem when attempting to overlay a grid. If I take the previous plot, and overlay a 10x10 grid using the grid() function, I get the following:
Note that the grid follows the entire plot window, not the fixed aspect-ratio plot. This makes it very difficult (read: nearly impossible) to overlay a regular grid on a fixed aspect-ratio plot. Does anyone know a good solution to this?
Edit: If you want code to run (there's really not much information there), here you go:
x <- 1:1200
y <- rep(x,times=800)
dim(y) = c(1200,800)
png(file="EXAMPLE1.PNG",width=1000,height=500)
image(y, asp=1)
dev.off()
png(file="EXAMPLE2.PNG",width=1000,height=500)
image(y, asp=1)
grid(col="black",nx=10,ny=10)
dev.off()
Upvotes: 0
Views: 711
Reputation: 184
Your x-axis is twice as long as the y-axis, so either use twice as many grid lines
png(file="EXAMPLE3.PNG",width=1000,height=500)
image(y, asp=1)
grid(20,10)
dev.off()
or use abline directly
png(file="EXAMPLE4.PNG",width=1000,height=500)
image(y, asp=1)
invisible(sapply(seq(-1,2,0.1),function(i){
abline(v=i,h=i,col = "lightgray", lty = "dotted",lwd = par("lwd"))
}
))
dev.off()
Upvotes: 1