R choropleth map of the Dominican Republic

779 views Asked by At

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

There are 2 answers

0
hrbrmstr On BEST 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

0
Ari On

I created an R package, choroplethrAdmin1, which was designed to simplify the creation of Administrative Level 1 choropleth maps of every country in the world in R.

library(choroplethr)
library(choroplethrAdmin1)

# ?admin1_map just draws an outline
admin1_map("dominican republic")

enter image description here

To create a choropleth, you need to to have a data.frame which contains two columns: one named region, one named value. The regions must be the names of the map.

# get a column named region, with the names of the regions
df = get_admin1_regions("dominican republic") 
df$value = 1:nrow(df) # add a column called "value"

# ?admin1_choropleth needs the name of the country and the data
admin1_choropleth(country.name = "dominican republic", df = df)

enter image description here