Message-ID: <9d4b0d44-4f90-3db8-363e-152f9574eb31@gmail.com>
Date: 2019-01-17T08:23:16Z
From: Bede-Fazekas Ákos
Subject: Simple choropleth using ggplot2
In-Reply-To: <5D1706E1-D552-4AC7-87E4-A894D181287A@gmail.com>
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 at data$id <- rownames(world.map at 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 at gmail.com
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>