Apparently there was a bug in the rgdal
package. My problem was solved after I updated it to version 0.8-12.
Shapefile and vector file don't precisely overlap when plotted
سؤال
I want to plot a shapefile over a raster file in R but I can't make them overlap perfectly: the raster appears to be rotated of few degrees counter-clockwise. Is it a problem with the projection?
Please consider the following MWE
library(raster)
library(rgdal)
# Download from http://biogeo.ucdavis.edu/data/gadm2/shp/ITA_adm.zip
shape_file = "ITA_adm1.shp"
# Download from http://sedac.ciesin.columbia.edu/data/set/gpw-v3-population-density/data-download
# Setting Geography: Country, Italy; Data Attributes: Grid
pop_density_file ="w001001.adf"
italy_map <- readOGR(dsn = shape_file, layer = "ITA_adm1")
italy_map_dens <- raster(pop_density_file)
colPal <- colorRampPalette(c("white", "red"))( 500 )
par(mar=c(0,0,0,0))
plot(italy_map_dens, xlim = c(6.70, 18.32), ylim = c(35.2, 47.6), axes=FALSE, box=FALSE, legend=FALSE, col=colPal)
plot(italy_map, xlim = c(6.70, 18.32), ylim = c(35.2, 47.6), border="grey", add=TRUE)
المحلول
نصائح أخرى
It does appear to be a problem with the projection. You need to find out the exact projection of both datasets, and convert one of the datasets to the projection of the other dataset.
لا تنتمي إلى StackOverflow