Illya Illya - 2 months ago 13
R Question

R choropleth map of the Dominican Republic

I would like to make a choropleth map of the Dominican Republic. As for boundaries, all I need are the states within that country.

I have tried the following:

map <- GetMap('Dominican Republic' , zoom = 8 , )
PlotOnStaticMap(map)
PlotPolysOnStaticMap(map , polys)

Answer

You can do this without RGoogleMaps. There are shapefiles & RData files at UC Davis for every region on the planet. The following annotated code:

  • downloads an RData SpatialPolygonsDataFrame of the admin level 1 areas of DR
  • grabs my theme_map
  • makes a sample data set
  • plots a choropleth with a proper map projection for the DR

You can use other actual shapefiles in similar fashion:

library(sp)
library(ggplot2)

# theme_map
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")

# the DR "shapefile"
download.file("http://biogeo.ucdavis.edu/data/gadm2/R/DOM_adm1.RData", "DOM_adm1.RData")
load("DOM_adm1.RData")

# for plotting with ggplot2
dr_map <- fortify(gadm)


# build some sample data, note that we'll be using
# the polygon "id" for plotting so you'll need that in the
# data frame
set.seed(1492)
choro <- data.frame(id=gadm@data$ID_1,
                    place=gadm@data$NAME_1,
                    value=sample(100, nrow(gadm@data)))

# you can look at the data with this (it won't be shown here)
head(choro)

# plot
gg <- ggplot()

# base map layer
gg <- gg + geom_map(data=dr_map, map=dr_map,
                    aes(x=long, y=lat, map_id=id),
                    fill="white", color="#7f7f7f", size=0.25)

# DR choro layer
gg <- gg + geom_map(data=choro, map=dr_map, 
                    aes(fill=value, map_id=id), 
                    color="#7f7f7f", size=0.25)

# lambert is a gd proj for DR
gg <- gg + coord_map("lambert", lat0=17.5, lat1=20)

# clean up map chart junk
gg <- gg + theme_map()

# move legend
gg <- gg + theme(legend.position="right")

# plot it
gg

enter image description here

Comments