Reputation: 83
I am having problems merging a Spanish provinces' shapefile with a particular dataframe. After merging them, somehow the shapes get linked to the false provinces (f.e. Barcelona returns the shape of Teruel). I have searched for examples, but I can not understand what I'm doing wrong.
All the necessary files are stored in this folder: Dropbox folder
library(maptools)
library(dplyr)
library(data.table)
library(reshape2)
I enter all the necessary files into R:
gor=readShapeSpatial('prov_map.shp')
prov=read.csv("prov.csv",sep = ';')
prov=subset(prov,select=-X)
I correct the province names in the datafile so they don't have a number before the actual name, then I turn the resulting variable into a factor:
provcorr=colsplit(prov$Province," ",c("Prov_num","Province"))
prov$prov_num=provcorr$Prov_num
prov$province_nonum=provcorr$Province
prov$provfact=as.factor(prov$province_nonum)
The province names in the shapefile are a little weird because of the spanish accents, so I correct them so they match the ones in the dataframe:
prov_nom=c("Melilla","Ceuta", "Zaragoza","Zamora", "Bizkaia",
"Valladolid","Valencia/València","Toledo","Teruel",
"Tarragona","Soria","Sevilla","Segovia","Cantabria",
"Salamanca","Pontevedra","Palencia","Asturias","Ourense",
"Navarra","Murcia","Málaga","Madrid","Lugo","Rioja, La",
"Lleida", "León", "Jaén", "Huesca", "Huelva", "Gipuzkoa",
"Guadalajara","Granada","Girona","Cuenca","Coruña, A",
"Córdoba", "Ciudad Real", "Castellón/Castelló","Cádiz",
"Cáceres", "Burgos","Barcelona", "Balears, Illes", "Badajoz",
"Ávila", "Almería", "Alicante/Alcant","Albacete","Araba/Álava")
gor@data$NAMEBIEN=as.factor(prov_nom)
Up to this point, the shapes are perfectly lined up with the province names. For example, if I type:
plot(subset(gor,gor@data$NAMEBIEN=='Barcelona'))
The shape of Barcelona shows up. But after I try to merge them with:
gor@data=merge(gor@data,prov,by.x='NAMEBIEN',by.y='provfact',all.x=T)
This is no longer the case. When I type the same code as before as in:
plot(subset(gor,gor@data$NAMEBIEN=='Barcelona'))
the shape of Teruel shows up.
The problem is driving me crazy. Any suggestions?
Upvotes: 4
Views: 230
Reputation: 4995
The problem is that merge changes the order of the data frame. In the original data frame Teruel is associated with the 9th polygon. But after merging the 9th entry in the data.frame is Barcelona and hence associated with the polygon of Teruel. Add the argument sort = FALSE
and it should work.
gor@data=merge(gor@data,prov,by.x='NAMEBIEN',by.y='provfact',all.x=T, sort = FALSE)
plot(subset(gor,gor@data$NAMEBIEN=='Barcelona'))
Upvotes: 2