Create a web page using R Markdown that features a map created with Leaflet.
Host your webpage on either GitHub Pages, RPubs, or NeoCities.
Your webpage must contain the date that you created the document, and it must contain a map created with Leaflet. We would love to see you show off your creativity!
For this assignmentment, I used R Markdown and Leaflet to construct a chloropleth map of global maternal mortality by country. Specifically, I graphed out each country’s Maternal Mortality Ratio, defined as “the number of women who die from pregnancy-related causes while pregnant or within 42 days of pregnancy termination per 100,000 live births.” My data were downloaded from Our World in Data, which has also visualized this metric of maternal health, among others.
I used the following packages:
First, we read in a geographic dataset from Thematic Mapping which allows us to overlay countries’ political borders onto our Leaflet map. Next, we read in the maternal mortality data, downloaded from Our World in Data. There are regions and countries not accounted for in the Thematic Mapping data, so these are omitted. Country names are recoded to agree with Thematic Mapping’s labeling.
The maternal mortality data is available for a range of different years as well, but for simplicity we display only the most recent (i.e., 2015).
# download Thematic Mapping data
#download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip", destfile = "world_shape_file.zip")
#system("unzip world_shape_file.zip")
world_spdf <- readOGR(
dsn = paste0(getwd(), "/world_shape_file/"),
layer = "TM_WORLD_BORDERS_SIMPL-0.3"
)
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/mike/Dropbox/coursera/data science/leaflet/world_shape_file", layer: "TM_WORLD_BORDERS_SIMPL-0.3"
## with 246 features
## It has 11 fields
## Integer64 fields read as strings: POP2005
# download maternal mortality data from Our World in Data
matmort <- read.csv("number-of-maternal-deaths.csv")
#str(matmort)
#head(matmort)
#as.data.frame(matmort)
# Recoding country names, filtering out unwanted entities
matmort <- matmort %>%
filter(Code != "" & Code != "OWID_WRL" & Entity != "South Sudan" & Year == 2015) %>%
rename(NAME = Entity) %>%
mutate(NAME = recode(NAME, "Brunei" = "Brunei Darussalam", "Democratic Republic of Congo" = "Democratic Republic of the Congo", "Iran" = "Iran (Islamic Republic of)", "Laos" = "Lao People's Democratic Republic", "Libya" = "Libyan Arab Jamahiriya", "Macedonia" = "The former Yugoslav Republic of Macedonia", "Micronesia (country)" = "Micronesia, Federated States of", "Moldova" = "Republic of Moldova", "Myanmar" = "Burma", "North Korea" = "Korea, Democratic People's Republic of", "South Korea" = "Korea, Republic of", "Tanzania" = "United Republic of Tanzania", "Timor" = "Timor Leste", "Vietnam" = "Viet Nam"))
# join the two data sets
world_spdf@data <- world_spdf@data %>%
full_join(matmort)
Next, we set up the legend and mouse-over functions for the chloropleth.
# setting parameters for chloropleth color scale
mybins <- c(0,1,5,10,50,100,500,1000,5000,Inf)
# setting mouseover text to show country name and mortality rate
mytext <- paste(
"Country: ", world_spdf@data$NAME,"<br/>",
"Deaths: ", round(world_spdf@data$X, 2),
sep="") %>%
lapply(htmltools::HTML)
The finished map is shown below:
mypalette <- colorBin(palette = "Reds",
domain = world_spdf@data$X,
na.color = "grey",
bins = mybins
)
matmort_map <- leaflet(world_spdf) %>%
addTiles() %>%
setView(lat = 10, lng = 0 , zoom = 2) %>%
addPolygons(
fillColor = ~mypalette(X),
stroke = TRUE,
fillOpacity = 0.6,
color = "black",
weight = 1.5,
label = mytext,
labelOptions = labelOptions(style = list(
"font-weight" = "normal",
padding = "3px 8px"),
textsize = "13px",
direction = "auto"
)) %>%
addLegend(pal = mypalette,
values = ~X,
opacity = 0.6,
position = "bottomleft",
title = "Deaths per 100 000 live births")
matmort_map