Thursday, 24 December 2015

Code to generate mapping for afghanistan in r

#Set Libraries

library(maps)
library(mapdata)
library(maptools)
library(scales)
library(gdata)

#Get the shapefile for Afghan Districts
area <- readShapePoly("/Users/Tim/Downloads/af_new/af.shp")

#Get data from Excel file
dataset<-read.table("/Users/Tim/Downloads/Workbook2.csv")
dataset2<-as.numeric(t(dataset))


#Plot outline map of Afghanistan
map("world","Afghanistan")

#Associate information with each of the data points in the shapefile
area@data$count <- dataset2

#Set the two colours you are varying between
rbPal <- colorRampPalette(bias=1,c('green','yellow'))

#Split the colours between the ranges of data between the colour choices
area@data$Col <- rbPal(4)[as.numeric(cut(area@data$count, seq(0, 2500, by = 625)))]

area@data$bin <- cut(area@data$count, seq(0, 2500, by = 625), 
      include.lowest = TRUE, dig.lab = 4)
lev = levels(area@data$bin)
lev2 <- gsub("\\,", " to ", lev)
lev3 <- gsub("\\]$", "", lev2)
lev4 <- gsub("\\(|\\)", " ", lev3)
lev5 <- gsub("^\\[", " ", lev4)
my.levels <- lev5

#Plot the map
plot(area,col=area@data$Col)
legend("topleft", fill = area@data$Col, legend = my.levels, col = area@data$Col)


No comments:

Post a Comment