Skip to content
Prev 19269 / 29559 Next

Google maps/ Blue Marble in R

Hello,

The next code combines three different objects using spplot. I have not
found the NBcontour10 data.frame in your last email. Therefore, I have
defined some points instead.

Best,

Oscar.

##################################################################
library(sp)
library(ggmap) 
##You could also try library(OpenStreetMaps)

## Read limit data and define an SpatialLines object
limit <- SpatialLines(list(Lines(Line(limit_NBver10[,-1]), ID='1')),
                      proj4string=CRS("+proj=longlat +datum=WGS84"))
## A component of sp.layout in the final spplot call
sp.limit <- list('sp.lines', limit, lwd=.5, col='white')

## Choose some points inside your region and define an
## SpatialPointsDataFrame object with some noise.
## Replace this chunk code with the definition of your data
bbLimit <- bbox(limit)
nPoints <- 20
coords <- cbind(sample(seq(bbLimit[1,1], bbLimit[1,2], .1), nPoints),
                sample(seq(bbLimit[2,1], bbLimit[2,2], .1), nPoints))
myPoints <- SpatialPointsDataFrame(coords,
                                   data=data.frame(rnorm(nPoints)),
                                   proj=CRS("+proj=longlat +datum=WGS84"))

## Retrieve tiles from Google Maps
map4ao <- get_map(location = c(lon = -30, lat = 15),
                  maptype = "satellite", scale=2, zoom=4)
## Read the attributes to pass them to grid.raster
bbMap <- attr(map4ao, '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)
## Another component of the sp.layout in spplot
sp.raster <- list('grid.raster', map4ao,
                  x=lonCenter, y=latCenter,
                  width=width, height=height,
                  default.units='native')

## Finally, the spplot call: the main object to be displayed is the
## SpatialPointsDataFrame. The region limits and the map layers are
## printed with sp.layout.
spplot(myPoints,
       scales=list(draw=TRUE), key.space='right',
       sp.layout=list(sp.raster, sp.limit))

##################################################################
zuzana zajkova writes: