简体   繁体   中英

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)

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

在此处输入图片说明

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

在此处输入图片说明

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM