r - Administrative regions map of a country with ggmap and ggplot2 -


i can make usa state level unemployment graph following code.

library(xml) library(ggplot2) library(plyr) library(maps)  unemp <-   readhtmltable('http://www.bls.gov/web/laus/laumstrk.htm',     colclasses = c('character', 'character', 'numeric'))[[2]]  names(unemp) <- c('rank', 'region', 'rate') unemp$region <- tolower(unemp$region)  us_state_map <- map_data('state') map_data <- merge(unemp, us_state_map, = 'region')  map_data <- arrange(map_data, order)  states <- data.frame(state.center, state.abb)  p1 <- ggplot(data = map_data, aes(x = long, y = lat, group = group)) p1 <- p1 + geom_polygon(aes(fill = cut_number(rate, 5))) p1 <- p1 + geom_path(colour = 'gray', linestyle = 2) p1 <- p1 + scale_fill_brewer('unemployment rate (jan 2011)', palette  = 'purd') p1 <- p1 + coord_map() p1 <- p1 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = null), size = 2) p1 <- p1 + theme_bw() p1 

enter image description here

now want similar kind of graph pakistan. few attempts results below:

data(world.cities) pakistan <- data.frame(map("world", "pakistan", plot=false)[c("x","y")])  p <- ggplot(pakistan, aes(x=x, y=y)) +      geom_path(colour = 'green', linestyle = 2) +      coord_map() + theme_bw() p <- p + labs(x=" ", y=" ") p <- p + theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) p <- p + theme(axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank()) p <- p + theme(panel.border = element_blank()) print(p) 

enter image description here

and

library(mapproj)  country <- "pakistan"  get_map_country <-   get_map(       location = country     , zoom = 5     , scale = "auto"     , maptype = "roadmap"     , messaging = false     , urlonly = false     , filename = "ggmaptemp"     , crop = true     , color = "color"     , source = "google"     , api_key     )  country1 <-   ggmap(       ggmap = get_map_country     , extent = "panel"   #  , base_layer     , maprange = false     , legend = "right"     , padding = 0.02     , darken = c(0, "black")     )  country1 <- country1 + labs(x="longitude", y="latitude") print(country1) 

enter image description here

country2 <- country1 + geom_polygon(data = pakistan                     , aes(x=x, y=y)                     , color = 'white', alpha = .75, size = .2)  print(country2) 

enter image description here

questions

i wonder how map of administrative regions of pakistan of usa. know need longitude , latitude of administrative boundaries. i'm wondering how longitude , latitude of administrative boundaries country. tried global administrative areas without success. in regard highly appreciated. thanks

i don't know spatial level of administrative areas require, here's 2 ways read in shapefile data , .rdata formats global administrative areas (gadm.org), , converting them data frames use in ggplot2. also, in order replicate u.s. map, need plot administrative area names located @ polygon centroids.

library(ggplot2) library(rgdal) 

method 1. spatialpolygondataframes stored .rdata format

# data global administrative areas # 1) read in administrative area level 2 data  load("/users/jmuirhead/downloads/pak_adm2.rdata") pakistan.adm2.spdf <- get("gadm") 

method2. shapefile format read in rgdal::readogr

pakistan.adm2.spdf <- readogr("/users/jmuirhead/downloads/pak_adm", "pak_adm2",   verbose = true, stringsasfactors = false) 

creating data.frame spatialpolygondataframes , merging data.frame containing information on unemployment, example.

pakistan.adm2.df <- fortify(pakistan.adm2.spdf, region = "name_2")  # sample dataframe of unemployment info unemployment.df <- data.frame(id= unique(pakistan.adm2.df[,'id']),   unemployment = runif(n = length(unique(pakistan.adm2.df[,'id'])), min = 0, max = 25))  pakistan.adm2.df <- merge(pakistan.adm2.df, unemployment.df, by.y = 'id', all.x = true) 

extracting names , centoids of administrative areas plotting

# centroids of spatialpolygondataframe , convert dataframe # use in plotting  area names.   pakistan.adm2.centroids.df <- data.frame(long = coordinates(pakistan.adm2.spdf)[, 1],     lat = coordinates(pakistan.adm2.spdf)[, 2])   # names , id numbers corresponding administrative areas pakistan.adm2.centroids.df[, 'id_2'] <- pakistan.adm2.spdf@data[,'id_2'] pakistan.adm2.centroids.df[, 'name_2'] <- pakistan.adm2.spdf@data[,'name_2'] 

create ggplot labels administrative areas

p <- ggplot(pakistan.adm2.df, aes(x = long, y = lat, group = group)) + geom_polygon(aes(fill = cut(unemployment,5))) + geom_text(data = pakistan.adm2.centroids.df, aes(label = name_2, x = long, y = lat, group = name_2), size = 3) +  labs(x=" ", y=" ") +  theme_bw() + scale_fill_brewer('unemployment rate (jan 2011)', palette  = 'purd') +  coord_map() +  theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) +  theme(axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank()) +  theme(panel.border = element_blank())  print(p) 

sample output pakistan unemployment


Comments

Popular posts from this blog

javascript - JS causing window size to be bigger than necessary - Dropdown bug -

php - Calling a template part from a post -

How to mention the localhost in android -