05/06/2020

Developing Data Products: Plotly

  • I used R Markdown and plotly to make a chloropleth map of global maternal mortality by country.
  • Specifically, I graphed out 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.”
  • Data was downloaded from Our World in Data, which has also visualized this metric of maternal health, among others.
  • I used the same data in the leaflet assignment and wanted to see if I could reproduce that chloropleth in plotly

R Code

# load libraries
library(plotly) 
library(rjson)
library(RColorBrewer)
library(tidyverse)

# read in csv data, focus on single year, transform to logarithmic scale
matmort <- read.csv("number-of-maternal-deaths.csv")
matmort <- matmort %>% filter(Year == 2015)
matmort$Xlog <- log10(matmort$X)
matmort$Xlog[matmort$Xlog < 0] <- 0

R Code

# set margins for map
m <- list(l = 50, r = 50, b = 100, t = 100, pad = 4)

# make plot
fig <- plot_ly(matmort, type = 'choropleth', locations = ~Code, z = ~Xlog, zmax = 5, hoverinfo = "text", text = ~paste("<br> Country: ", Entity, "<br> Deaths: ", X), colorscale = "Reds")
fig <- fig %>%
        colorbar(title = "Deaths per 100 000 live births", len = 0.75, thickness = 20, y = 0.9, tickmode = "array", tickvals = list(0, 0.7, 1, 1.7, 2, 2.7, 3, 3.7, 4, 4.7, 5), ticktext = list(0, 5, 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000)) %>%
        layout(autosize = F, width = 800, height = 500, margin = m)

Figure 1. Maternal mortality ratio in 2015