[R-sig-Geo] Simple choropleth using ggplot2

Bede-Fazekas Ákos b|@|ev||@t @end|ng |rom gm@||@com
Thu Jan 17 09:23:16 CET 2019


Dear Richard,
although I have problem with your mmr.zip (R cannott unzip it), and 
could not try your code, I  suggest to migrate from sp to sf. ggplot can 
easily use Simple Features data.
Use function sf::st_as_sf() to convert your Spatial* data to Simple 
Features.
HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences

2019.01.17. 3:19 keltezéssel, Richard Sherman írta:
> Hello all,
>
> I am trying to plot a simple choropleth, something I’ve done a while ago using rworldmap and also (if I recall correctly) ggplot2, but I am failing to draw the map at all and failing (I think) to merge my data properly with the shapefile. Thank you for help with a basic question. I’d like to know what is wrong with what I’m doing here.
>
> My R script is
>
> library(ggplot2)
> library(rgdal)
> library(plyr)
>
> # get shapefile for world map
> download.file("https://opendata.arcgis.com/datasets/252471276c9941729543be8789e06e12_0.zip", destfile = "countries.zip”)
>
> # get world bank maternal mortality data
> download.file("http://api.worldbank.org/v2/en/indicator/SH.STA.MMRT?downloadformat=csv", destfile = "mmr.zip”)
>
> # get csv file with concordance between ISO-2-alpha and ISO-3-alpha country codes
> download.file("https://raw.githubusercontent.com/rsspdx/mmr/master/iso_2_iso_3.csv", destfile = "iso_2_iso_3.csv”)
>
> # unzip the zipped files
> mmr.files <- unzip("mmr.zip")
> unzip("countries.zip”)
>
> # read in maternal mortality data and fix it up
> mmr.data <- read.csv(mmr.files[2], skip = 3, stringsAsFactors = FALSE)
> mmr.data.name <- mmr.data$Country.Name
> mmr.data.code <- mmr.data$Country.Code
> mmr.data.mmr <- mmr.data$X2015
> mmr.data.df <- as.data.frame(cbind(mmr.data.name, mmr.data.code, mmr.data.mmr))
> names(mmr.data.df) <- c("Country.Name", "Country.Code", "mmr”)
>
> # read in the shapefile
> world.map <- readOGR(dsn=".", layer = "UIA_World_Countries_Boundaries")
>
> # --------- possibly I should be doing this ------------
> #
> # world.map using data$id <- rownames(world.map using data)
> # world.map.df <- fortify(world.map)
> #
> # -------------------------------------------------------
>
> #------or perhaps I need to merge the data into a data slot of the shapefile
> #------but I can’t recall (or never knew?) how to do that
>
> # get ISO2 country codes
> iso_2_iso_3 <- read.csv("iso_2_iso_3.csv”)
>
> # ISO2 in this file is called ISO in the shapefile, create ISO variable
> # then merge into mmr.data
> iso_2_iso_3$ISO <- iso_2_iso_3$ISO2
> mmr.data.df <- merge(iso_2_iso_3, mmr.data.df, by.x="ISO3", by.y="Country.Code”)
>
> # merge maternal mortality data into shapefile
> mmr <- merge(world.map, mmr.data.df, by = "ISO")
> mmr <- fortify(mmr)
> str(mmr)
>
> # ---------create a map, not working
> map <- ggplot(data = mmr, aes(x = long, y = lat, group = group))
>
> # ---------look at the map, obviously not working
> map + geom_polygon(fill = mmr$mmr)
>
>
> ---
> Richard Sherman
> rss.pdx using gmail.com
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list