[R] map() and pdf clipping

Ben Tupper ben.bighair at gmail.com
Mon Nov 29 20:03:15 CET 2010


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/



More information about the R-help mailing list