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.
I posted this to r-sig-geo but received no responses.
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 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)