[R-sig-Geo] Excessive whitespace in saved images from sp::plot()

Matt Strimas-Mackey strimas at zoology.ubc.ca
Tue Dec 15 19:53:00 CET 2015


I'm trying to plot simple SpatialPolygons objects using the basic
plot() command from sp and save these as png or svg. For context, this
is part of a rmarkdown report that will eventually go on the web, so
the saving of images will be done automatically via knitr. Since this
involves images, I've put the code and images online:
https://github.com/mstrimas/mstrimas.github.io/tree/master/_source/print-problems

My problem is excessive whitespace around the plots:

library(sp)
par(mar=c(0, 0, 0, 0))
png('png-dev-400x400.png', width = 400, height = 400)
plot(square, axes = F)
dev.off()

But only when saving with a direct call to png() or svg(). Plotting,
then saving interactively via RStudio or with

dev.print(png, 'dev-print-400x400.png', width = 400, height = 400)

yields a nice plot with no whitespace.

I only encounter this problem when plotting spatial objects, which is
why I'm posting here. Since these are simple polygons with no
attributes, I'd prefer to avoid using spplot(). Also, I've tried this
on 2 machines: a Mac and a PC running Ubuntu, in both cases running
the latest version of R and the sp package.

Thanks!

M



More information about the R-sig-Geo mailing list