Question

I am wondering how to draw a map using get_map and ggmap of any federal country (i.e. a country with provinces and counties). Any country, other than the US would be great. To make it look nice, fill the geom_polygon of counties (any fill), and provinces are empty polygons, only with its contours. So, basically, it is two overlapping ggmaps.

You can get the shapefiles here:

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),]

The following code produces counties boundaries:

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

Now, provinces:

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

How to make the two work together?

Was it helpful?

Solution

You could also get your maps from e.g. GADM:

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

And let us fortify those for ggplot usage:

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

And add as many layers and geoms as you wish:

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()

Resulting in:

enter image description here


Update: combining your two layers mentioned in the updated questions

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top