Choropleth using ggplot2

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Choropleth using ggplot2

Richard Sherman
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


# get shapefile for world map
download.file("", destfile = "”)

# get world bank maternal mortality data
download.file("", destfile = "”)

# get csv file with concordance between ISO-2-alpha and ISO-3-alpha country codes
download.file("", destfile = "iso_2_iso_3.csv”)

# unzip the zipped files
mmr.files <- unzip("")

# read in maternal mortality data and fix it up <- read.csv(mmr.files[2], skip = 3, stringsAsFactors = FALSE) <-$Country.Name <-$Country.Code <-$X2015 <-,,
names( <- c("Country.Name", "Country.Code", "mmr”)

# read in the shapefile <- readOGR(dsn=".", layer = "UIA_World_Countries_Boundaries")

# --------- possibly I should be doing this ------------
#$id <- rownames(
# <- fortify(
# -------------------------------------------------------

#------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
iso_2_iso_3$ISO <- iso_2_iso_3$ISO2 <- merge(iso_2_iso_3,, by.x="ISO3", by.y="Country.Code”)

# merge maternal mortality data into shapefile
mmr <- merge(,, by = "ISO")
mmr <- fortify(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)

[hidden email] mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.