I'm trying to plot some Turkish data using R.
The problem I'm having is when I merge my data with the shape file (in spatialpolygonsdataframe format) the data no longer matches the correct pologons. What am I doing wrong,
Below is some reproducible code. The shape file is some natural earth data (so public domain) and I've put it on my google drive zipped with the simple data excel file. It produces 2 plots with the province name plotted, before and after the merge. You can see that the second image has "jumbled" the data and the Turkey.map@data no longer matches the correct polygon.
Before merge plot with correct province names:
After merge plot:
library(maptools)
library(readxl)
temp <- "TurkeyShapefile.zip"
URL <- "https://docs.google.com/ucid=0B0TyKM0aACIONUxfNTJwWGhrR0k&export=download"
download.file(URL,temp, mode="wb")
unzip(temp)
trtr <- readShapeSpatial("Natural_earth_admin_RMS150518_TR")
#read excel file
fname <- "TR_data.xlsx"
TRdata <- read_excel(fname, sheet = "pcnt")
Turkey.map <- trtr #create copy of trtr
#a plot of the map before the merge
plot(Turkey.map)
invisible(text(getSpPPolygonsLabptSlots(Turkey.map), labels=as.character(Turkey.map@data$Admin1Name), cex=0.5))
#merge (join data)
Turkey.map@data <- merge(Turkey.map@data,TRdata,by.x="Admin1Name",by.y="Province", all.x=TRUE)
#a plot of the map after the merge
plot(Turkey.map)
invisible(text(getSpPPolygonsLabptSlots(Turkey.map), labels=as.character(Turkey.map@data$Admin1Name), cex=0.5))
Many thanks!