Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An R package for India?

Tags:

r

ggplot2

I am doing lots of statistical analysis on county basis in R for US. But I want to do some studies for India as well. I have found state map, but no district map in R. I can find such things in d3.js but I would rather not abandon R.

Is there a R package for India that is similar to 'maps'.

like image 415
Geekuna Matata Avatar asked Apr 10 '14 19:04

Geekuna Matata


1 Answers

You can use data from GADM which contains shapefiles on different levels of administrative division so also district level which is level 2 I guess. You can use the script below to directly load the data, code is taken from here.

So in your case you would run:

IND<-getCountries("IND",level=2)

Just to check, plot the data:

plot(ind)

enter image description here

Alternatively you can use GAUL data and load the shapefile using maptools.

Code to get data.

# Load required libraries
library(sp)

# Load file from GADM
# Specify the countries for fileName using ISO3C
# like "AFG" for Afghanistan.
# "level" specifies adminsitrative level.
loadGADM<-function(fileName,level=0,...){
load(url(paste("http://gadm.org/data/rda/",fileName,"_adm",level,".RData",sep = "")))
gadm
}

# Add prefix (ISO3C code) to shapefile.
changeGADMPrefix<-function(GADM, prefix) {
GADM <- spChFIDs(GADM, paste(prefix, row.names(GADM), sep = "_"))
GADM
}

# Load file and change prefix
loadChangePrefix<-function (fileName, level = 0, ...) {
theFile <- loadGADM(fileName, level)
theFile <- changeGADMPrefix(theFile, fileName)
theFile
}

# Apply all the functions:
getCountries <- function (fileNames, level = 0, ...) {
polygon <- sapply(fileNames, loadChangePrefix, level)
polyMap <- do.call("rbind", polygon)
polyMap
}
like image 155
horseoftheyear Avatar answered Sep 27 '22 18:09

horseoftheyear