Last active
December 28, 2015 09:59
-
-
Save oscarperpinan/7482848 to your computer and use it in GitHub Desktop.
Variation of https://gist.github.com/oscarperpinan/5451682 using SpatialPolygons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sp) | |
library(ggmap) | |
## latticeExtra must be loaded after ggmap because both ggplot2 and | |
## latticeExtra define a 'layer' function. We need the definition from | |
## latticeExtra. | |
library(latticeExtra) | |
## We only need maptools to get an example | |
library(maptools) | |
SIDS <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], | |
IDvar="FIPSNO", | |
proj4string=CRS("+proj=longlat +ellps=clrk66")) | |
## Get the stamen map. Same as with SpatialPoints | |
bbPoly <- bbox(SIDS) | |
gmap <- get_map(c(bbPoly), maptype='watercolor', source='stamen', crop=FALSE) | |
bbMap <- attr(gmap, 'bb') | |
latCenter <- with(bbMap, ll.lat + ur.lat)/2 | |
lonCenter <- with(bbMap, ll.lon + ur.lon)/2 | |
height <- with(bbMap, ur.lat - ll.lat) | |
width <- with(bbMap, ur.lon - ll.lon) | |
## Use latticeExtra::+.trellis and latticeExtra::layer instead of sp.layout | |
spplot(SIDS['BIR79']) + | |
layer(grid.raster(gmap, | |
x=lonCenter, y=latCenter, | |
width=width, height=height, | |
default.units='native'), | |
under=TRUE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## If you need to use layer inside a function, you have to evaluate the code inside "layer({})" | |
## or pass the arguments as a list in 'data'. | |
myFoo <- function(){ | |
spplot(SIDS['BIR79']) + | |
layer({ | |
bbPoly <- bbox(SIDS) | |
gmap <- get_map(c(bbPoly), maptype='watercolor', source='stamen', crop=FALSE) | |
bbMap <- attr(gmap, 'bb') | |
latCenter <- with(bbMap, ll.lat + ur.lat)/2 | |
lonCenter <- with(bbMap, ll.lon + ur.lon)/2 | |
height <- with(bbMap, ur.lat - ll.lat) | |
width <- with(bbMap, ur.lon - ll.lon) | |
grid.raster(gmap, | |
x=lonCenter, y=latCenter, | |
width=width, height=height, | |
default.units='native') | |
}, under=TRUE) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myFoo2 <- function(){ | |
bbPoly <- bbox(SIDS) | |
gmap <- get_map(c(bbPoly), maptype='watercolor', source='stamen', crop=FALSE) | |
bbMap <- attr(gmap, 'bb') | |
latCenter <- with(bbMap, ll.lat + ur.lat)/2 | |
lonCenter <- with(bbMap, ll.lon + ur.lon)/2 | |
height <- with(bbMap, ur.lat - ll.lat) | |
width <- with(bbMap, ur.lon - ll.lon) | |
spplot(SIDS['BIR79']) + | |
layer({ | |
grid.raster(gmap, | |
x=lonCenter, y=latCenter, | |
width=width, height=height, | |
default.units='native') | |
}, under=TRUE, | |
data=list(gmap=gmap, | |
lonCenter=lonCenter, latCenter=latCenter, | |
width=width, height=height)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myFoo3 <- function(poly){ | |
bbPoly <- bbox(poly) | |
gmap <- get_map(c(bbPoly), maptype='watercolor', | |
source='stamen', crop=FALSE) | |
bbMap <- attr(gmap, 'bb') | |
latCenter <- with(bbMap, ll.lat + ur.lat)/2 | |
lonCenter <- with(bbMap, ll.lon + ur.lon)/2 | |
height <- with(bbMap, ur.lat - ll.lat) | |
width <- with(bbMap, ur.lon - ll.lon) | |
spplot(poly) + | |
layer({ | |
grid.raster(gmap, | |
x=lonCenter, y=latCenter, | |
width=width, height=height, | |
default.units='native') | |
}, under=TRUE, | |
data=list(gmap=gmap, | |
lonCenter=lonCenter, latCenter=latCenter, | |
width=width, height=height)) | |
} | |
myFoo3(SIDS["BIR79"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myFoo4 <- function(poly, map, ...){ | |
bbMap <- attr(map, 'bb') | |
latCenter <- with(bbMap, ll.lat + ur.lat)/2 | |
lonCenter <- with(bbMap, ll.lon + ur.lon)/2 | |
height <- with(bbMap, ur.lat - ll.lat) | |
width <- with(bbMap, ur.lon - ll.lon) | |
spplot(poly, ...) + | |
layer({ | |
grid.raster(map, | |
x=lonCenter, y=latCenter, | |
width=width, height=height, | |
default.units='native') | |
}, under=TRUE, | |
data=list(map=map, | |
lonCenter=lonCenter, latCenter=latCenter, | |
width=width, height=height)) | |
} | |
mapWater <- get_map(c(bbox(SIDS)), maptype='watercolor', | |
source='stamen', crop=FALSE) | |
myFoo4(SIDS["BIR79"], mapWater, | |
col.regions=terrain.colors(30)) | |
mapTerrain <- get_map(c(bbox(SIDS)), maptype='terrain', | |
source='stamen', crop=FALSE) | |
myFoo4(SIDS["BIR79"], mapTerrain, | |
col.regions=terrain.colors(30)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Watercolor stamen maps are now in jpeg format. Version 2.4 of ggmap fixes this: dkahle/ggmap@d657e1c.