2015-08-28 116 views
2

我想擺脫SpatialPolygons繪圖的頂部和底部邊距。 我曾嘗試將邊距設置爲c(0,0,0,0),但這隻會改變左右邊距。刪除PNG繪圖邊距

在RStudio中繪圖時,頂部和底部邊距爲0,但左側和右側不是。

library(sp) 

coords <- cbind(c(631145, 631757, 631928, 631664, 631579, 631281), 
       c(6967640, 6967566, 6968027, 6967985, 6968141, 6968009)) 
poly <- Polygons(list(Polygon(coords)),"coords") 
poly.sp <- SpatialPolygons(list(poly)) 

par(mar = rep(0, 4), xaxs='i', yaxs='i') 
plot(poly.sp, bg="yellow") 

png('poly.png') 
par(mar = rep(0, 4), xaxs='i', yaxs='i') 
plot(poly.sp, bg="yellow") 
dev.off() 

plot

回答

2

我通過計算我試圖繪製,然後設置情節的寬度和高度的多邊形的縱橫比解決了這個問題。

這可能不是最優雅的解決方案,但它可以完成這項工作。

library(sp) 

coords <- cbind(c(631145, 631757, 631928, 631664, 631579, 631281), 
       c(6967640, 6967566, 6968027, 6967985, 6968141, 6968009)) 
poly <- Polygons(list(Polygon(coords)),"coords") 
poly.sp <- SpatialPolygons(list(poly)) 

width <- [email protected][3] - [email protected][1] 
height <- [email protected][4] - [email protected][2] 
aspect <- height/width 

png('poly.png', width = 10, height = 10*aspect, units = 'in', res = 300) 
par(mar = rep(0, 4), xaxs='i', yaxs='i') 
plot(poly.sp, bg="yellow") 
dev.off() 
+0

很棒的工作! – Andy