如何绘制具有两个不同管理边界的ggmap?

时间:2013-11-01 00:13:36

标签: r ggmap

我想知道如何使用任何联邦国家(即有省和县的国家)的get_mapggmap绘制地图。任何国家,除美国外都会很棒。要使它看起来不错,请填写geom_polygon个县(任何填充),省份是空的多边形,只有它的轮廓。所以,基本上,它是两个重叠的ggmaps。

您可以在此处获取shapefile:

https://www.dropbox.com/s/4nl685t860x1e8r/municipios_br.zip

rm(list = ls())
library(ggplot2)
library(rgdal)
library(ggmap)

# READ SHAPEFILE OF BOUNDARIES
Map <- readShapePoly("municipios_br.shp")

head(as.data.frame(Map))
Map = gBuffer(Map, width=0, byid=TRUE)
MapC <- fortify(Map, region="CODIGO_MUN") # municipalities
MapP <- fortify(Map, region="CODIGO_UF") # state boundaries
MapC$test <- 1
MapP$test <- 1
MapC <- Map[order(MapC$order),]
MapP <- MapP[order(MapP$order),]

以下代码生成县边界:

google.map <- get_map(location = 'Brazil', zoom=4,maptype="terrain")
m0 <- ggmap(google.map)
m1 <- m0 + geom_polygon(color = 'grey90', size = .01, aes(x=long, y=lat, group=group, fill=as.factor(test)), data=MapC, alpha=.6)
m1 + guides(fill=FALSE) + scale_fill_manual(values=c("red"))

Map - counties

现在,各省:

m2 <- m0 + geom_polygon(color = 'grey50', size = .1, aes(x=long, y=lat, group=group, fill=as.factor(test)), data=MapP, alpha=.9)
m2 + guides(fill=FALSE) + scale_fill_manual(values=c(NA))

Map - provinces

如何让两者协同工作?

1 个答案:

答案 0 :(得分:5)

您也可以从中获取地图。 GADM

library(raster)
adm1 <- getData('GADM', country='HUN', level=0)
adm2 <- getData('GADM', country='HUN', level=1)

让我们fortify ggplot用法:

library(ggplot2)
fadm1 = fortify(adm1)
fadm2 = fortify(adm2)

并添加任意数量的图层和几何图形:

ggplot(fadm1, aes(x = long, y = lat, group = group)) + geom_path() +
    geom_polygon(data = fadm2, aes(x = long, y = lat), 
                 fill = "green", alpha = 0.5) +
    geom_path(data = fadm2, aes(x = long, y = lat), color = "blue") + 
    theme_bw()

导致:

enter image description here


更新:结合更新后的问题中提到的两个层次

m0 + geom_polygon(size = .01,
        aes(x = long, y = lat, group = group, fill = as.factor('red')),
        data = MapC,
        alpha = .6) +
    geom_path(color = 'grey50', size = .1, aes(x = long, y = lat, group = group),
        data=MapP, alpha=.9) +
    guides(fill=FALSE)

enter image description here