Reputation: 12188
I have administrative boundary data from the Global Administrative Areas Database. Plotting subdivisions with this data is very easy. For example, if I want to overplot geocoded data contained in the file mydata
on counties in Florida, I download the "level 2" for the US, and then use this code:
load("/home/anindya/Desktop/DELETE/USA_adm2.RData")
temp = as.data.frame(gadm) *Code for eyeballing structure of data
florida = gadm[344:410,]
plot(florida); points(mydata$longitude, mydata$latitude)
But how do you overlay other information on this, say for topography and waterways? I am particularly interested in data from Natural Earth and the Global Lakes and Wetlands Database.
FOLLOW-UP EDIT: Many thanks to Paul for a soloution to the problem. I had crossposted a variation of this question on here at gis.stackexchange, where R.K. gave an excellent answer. Do read up on it as well.
Upvotes: 1
Views: 1384
Reputation: 2905
If I understand you correctly you are asking about how to superimpose different geographic layers as one would using a GUI GIS.
This is straightforward if the data is in a Spatial*
object (e.g. SpatialPoints, SpatialLines, SpatialPolygons
etc. from the sp
package) or as a Raster*
object from the raster
package. The plot()
methods in both of these packages can handle this superimposition if the parameter add=TRUE
is given.
To get your vector-based geographic data into Spatial*
objects you can use functions from the maptools
package (e.g. readShapePoly()
reads ESRI polygon shapefiles). Rasters from a variety of file specifications can be loaded using raster()
and additional formats are available if the rgdal
package is installed.
Here is an example of superimposing geographic data types using fabricated geographic data. First create some example data on a UTM grid:
library(sp)
library(raster)
## Create a RasterLayer object and fill with random values
baseRaster <- raster(nrow=100, ncol=100,
xmn=0, xmx=100, ymn=0, ymx=100,
crs= "+proj=utm")
baseRaster[] <- runif(ncell(baseRaster))
## Create a second raster to superimpose
## It should contain NA values where it is transparent
supRaster <- baseRaster
supRaster[] <- NA
supRaster[cellFromCol(supRaster, 48:52)] <- 1
## Create SpatialPoints object to superimpose on these
loc <- SpatialPoints(cbind(seq(10, 90, by=10), seq(10, 90, by=10)))
Now do the plots:
## Plot base raster
plot(baseRaster)
## Superimpose second raster in a different colour
## Turn off legend
plot(supRaster, add=TRUE, col="blue", legend=FALSE)
## Superimpose points and make them big and colourful
plot(loc, add=TRUE, pch=20, cex=3, col="red")
Here is the result:
Upvotes: 3