具有ggmap和ggplot2的国家/地区的行政区域地图

时间:2013-07-18 12:45:07

标签: r ggplot2 ggmap

我可以使用以下代码制作美国州级失业率图表。

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, by = '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

现在我想要为巴基斯坦提供类似的图表。我的几次尝试结果如下:

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

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

问题

我想知道如何获得美国的巴基斯坦行政区域地图。我知道,我们需要行政边界的经度和纬度。我想知道如何获得一个国家的行政边界的经度和纬度。我试过Global Administrative Areas但没有成功。在这方面的任何帮助将受到高度赞赏。感谢

2 个答案:

答案 0 :(得分:47)

我不知道您需要的行政区域的空间级别,但这有两种方式 从Global Administrative Areas (gadm.org)读取shapefile数据和.RData格式,并将它们转换为数据帧以便在ggplot2中使用。此外,为了复制美国地图,您需要绘制位于多边形质心的管理区域名称。

library(ggplot2)
library(rgdal)

方法1.将SpatialPolygonDataFrames存储为.RData格式

# Data from the Global Administrative Areas
# 1) Read in administrative area level 2 data

load("/Users/jmuirhead/Downloads/PAK_adm2.RData")
pakistan.adm2.spdf <- get("gadm")

方法2。使用rgdal :: readOGR

读入Shapefile格式
pakistan.adm2.spdf <- readOGR("/Users/jmuirhead/Downloads/PAK_adm", "PAK_adm2", 
 verbose = TRUE, stringsAsFactors = FALSE)

从spatialPolygonDataframes创建data.frame并与包含失业信息的data.frame合并,例如。

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)

提取行政区域的名称和中心点以进行绘图

# Get centroids of spatialPolygonDataFrame and convert to dataframe
# for use in plotting  area names. 

pakistan.adm2.centroids.df <- data.frame(long = coordinates(pakistan.adm2.spdf)[, 1], 
   lat = coordinates(pakistan.adm2.spdf)[, 2]) 

# Get names and id numbers corresponding to 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']

使用行政区域标签

创建ggplot
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 for Pakistan unemployment

答案 1 :(得分:3)

你试过http://www.diva-gis.org/gdata吗?这应该给你Shapefiles与巴基斯坦的州/区边界。

maptools包具有的功能可让您读取Shapefile并将其转换为可用于布局边界多边形的数据框。