[R] map() and pdf clipping

Paul Murrell p.murrell at auckland.ac.nz
Wed Dec 1 22:31:54 CET 2010


Hi

On 1/12/2010 4:44 a.m., Peter Ehlers wrote:
> On 2010-11-30 04:56, Ben Tupper wrote:
>> Hi,
>>
>> I thought it might help if I posted the resulting images.
>>
>> This is the pdf file where the map polygons are not clipped to the
>> plotting boundary.
>>
>> http://dl.dropbox.com/u/8433654/test-map.pdf
>>
>> And this is the png file showing the same polygons clipped to the
>> plotting boundary which is what I hope to see in the pdf, too.
>>
>> http://dl.dropbox.com/u/8433654/test-map.png
>>
>>
>> I suspect that there is some simple parameter or sequence I am
>> missing... something that a good-heatred dope slap will solve!
>>
> Hi Ben,
> I haven't figured out why this occurs - seems to be something
> to do with the way rasterImage and pdf play together. But this

Correct.  The code for drawing raster images on the PDF device was being 
selfish with the clipping region.  A fix has been committed.

Paul

> works for me: insert the following line
>
>    do.call("clip", as.list(par('usr')))
>
> before your final map call.
>
> Peter Ehlers
>
>> Thanks,
>>
>> Ben
>>
>>
>>
>> On Nov 29, 2010, at 2:03 PM, Ben Tupper wrote:
>>
>>> Hello,
>>>
>>> Below is a function (test.map) that permits drawing the same map
>>> using three different devices.  The "pdf" device doesn't clip
>>> polygons to the plot region as I see it does by both the native
>>> device (in my case "Quartz") and the "png" device.
>>>
>>> test.map("pdf")  # produces "test-map.pdf" with no clipping
>>> test.map("png")  # produces "test-map.png" with clipping
>>> test.map(NA) 	# draws on the window device with clipping
>>>
>>> It doesn't appear to matter what the value of the fill argument is -
>>> the pdf output shows that the polygons are not being clipped to the
>>> plot region.
>>>
>>> I have viewed the pdf output using Mac OSX's Preview, PDFPen, Adobe
>>> Reader and Safari and they all render the same way. So my hunch is
>>> that it is not a viewer issue (although I suppose they might be
>>> using the same rendering engine under the hood.)
>>>
>>> Any help would be greatly appreciated.
>>>
>>> Thanks and cheers,
>>> Ben
>>>
>>> ###### BEGIN
>>> library(maps)
>>>
>>> test.map<- function(to.file = c("pdf", "png", NA)[1], fill = TRUE){
>>>
>>>     if (!is.na(to.file)){
>>>        ofile = paste("test-map", to.file,sep = ".")
>>>        do.call(to.file, list(file=ofile))
>>>     }
>>>     xr<- c(-185, -155)
>>>     yr<- c(45, 70)
>>>
>>>     map(xlim = xr, ylim = yr)
>>>     map.axes()
>>>
>>>     m<- matrix(seq(0, 1, length = 40*40), nrow = 40)
>>>     mr<- as.raster(m)
>>>     rasterImage(m, -180, 50, -160, 65)
>>>
>>>     map(xlim = xr, ylim = yr, fill = fill, add = TRUE)
>>>
>>>     if (!is.na(to.file)){
>>>        cat("wrote:", ofile, "\n")
>>>        dev.state<- dev.off()
>>>     }
>>>
>>> }
>>>
>>>
>>> ###### END
>>>
>>>
>>>> sessionInfo()
>>> R version 2.12.0 (2010-10-15)
>>> Platform: i386-apple-darwin9.8.0/i386 (32-bit)
>>>
>>> locale:
>>> [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
>>>
>>> attached base packages:
>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>> other attached packages:
>>> [1] mapproj_1.1-8.2    akima_0.5-4        RColorBrewer_1.0-2
>>> [4] mapdata_2.1-3      maps_2.1-5
>>>
>>> loaded via a namespace (and not attached):
>>> [1] tools_2.12.0
>>>
>>>
>>>
>>> Ben Tupper
>>> Bigelow Laboratory for Ocean Sciences
>>> 180 McKown Point Rd. P.O. Box 475
>>> West Boothbay Harbor, Maine   04575-0475
>>> http://www.bigelow.org/
>>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list