Skip to content
Prev 27487 / 29559 Next

how to specify the size of device so that it fits a SpatialPolygonsDataFrame plot exactly

On Sun, 28 Jul 2019, Patrick Giraudoux wrote:

            
Yes, and the section in the book only discusses projected objects, as 
geographical coordinates are stretched N-S proportionally to the distance 
from the Equator. For the UTM47 object, I have:

library(sp)
bbChinaUTM47 <- 
SpatialPolygons(list(Polygons(list(Polygon(matrix(c(-2331000.09677063, 
-2331000.09677063, 4043212.88065733, 4043212.88065733, -2331000.09677063, 
1912947.1678777, 6721582.03465746, 6721582.03465746, 1912947.1678777, 
1912947.1678777), ncol=2))), ID="1")), proj4string=CRS("+proj=utm 
+zone=47")) # you had longlat, so triggering the stretching.

x11() # or equivalent
dxy <- apply(bbox(bbChinaUTM47), 1, diff)
dxy
ratio <- dxy[1]/dxy[2]
ratio
pin <- par("pin")
pin
par(pin=c(ratio * pin[2], pin[2]), xaxs="i", yaxs="i")
plot(bbChinaUTM47)
box()

where the box overlaps the SP object. To finesse:

c(ratio * pin[2], pin[2])
dev.off()
X11(width=6.85, height=5.2)
par(mar=c(0,0,0,0)+0.1)
pin <- par("pin")
par(pin=c(ratio * pin[2], pin[2]), xaxs="i", yaxs="i")
plot(bbChinaUTM47)
box()
dev.off()