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

Edzer Pebesma edzer.pebesma at uni-muenster.de
Wed Dec 16 22:20:32 CET 2015



On 16/12/15 19:35, Matt Strimas-Mackey wrote:
> 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!

https://en.wikipedia.org/wiki/Side_effect_(computer_science)

> 
> 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]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Edzer Pebesma
Institute for Geoinformatics  (ifgi),  University of Münster
Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
Journal of Statistical Software:   http://www.jstatsoft.org/
Computers & Geosciences:   http://elsevier.com/locate/cageo/
Spatial Statistics Society http://www.spatialstatistics.info

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20151216/c2ff1562/attachment.bin>


More information about the R-sig-Geo mailing list