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

Matt Strimas-Mackey strimas at zoology.ubc.ca
Wed Dec 16 19:35:29 CET 2015


After messing around with parameters aimlessly I managed to solve my
problem.

The key seems to be that par(mar=c(0, 0, 0, 0)) needs to come AFTER
png('plot.png')
as in:
png('plot.png')
par(mar=c(0, 0, 0, 0))
plot(square, axes = F, lwd = 2, asp = "", xpd = NA)
dev.off()

I have no idea why this is the case, but it works!

M

On Tue, Dec 15, 2015 at 2:22 PM, Matt Strimas-Mackey <strimas at zoology.ubc.ca
> wrote:

> Thanks for the suggestions.
>
> Here's what I tried based on what you said:
> par(mar=c(0, 0, 0, 0))
> png('plot.png')
> plot(square, axes = F, lwd = 2, asp = "", xpd = NA)
> dev.off()
>
> Unfortunately, this didn't make any difference...  Any other thoughts?
>
>
> Thanks!
>
> M
>
> On Tue, Dec 15, 2015 at 11:48 AM, Michael Sumner <mdsumner at gmail.com>
> wrote:
>
>> Try changing the aspect ratio, asp="" from memory to 'fill the device'.
>> You might also need par(xpd=NA) or similar to remove the axis buffering.
>>
>> R and sp generally plot within a figure space, rendering to a "world
>> space" within a file is not directly supported (writeGDAL and writeRaster
>> do this, but not for rendering 'layers'). But with careful handling you can
>> do it.
>>
>> I think external georeferencing would be required for downstream spatial
>> use, but I am hoping to be corrected on that point(?)
>>
>> Cheers, Mike
>>
>>
>> On Wednesday, December 16, 2015, Matt Strimas-Mackey <
>> strimas at zoology.ubc.ca> wrote:
>> > 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
>> >
>> > _______________________________________________
>> > R-sig-Geo mailing list
>> > R-sig-Geo at r-project.org
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> >
>>
>> --
>> Michael Sumner
>> Software and Database Engineer
>> Australian Antarctic Division
>> Hobart, Australia
>> e-mail: mdsumner at gmail.com
>>
>
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list