This is the code accompanying my post “Should you visualize data with a map or a chart? Thoughts from teaching introductory geography” available at http://walkerke.github.io/2015/04/map-or-chart/. The code below will create a Leaflet choropleth map and a Stephanie Evergreen-style dot plot with ggplot2 of World Bank Gini index data.

Data setup:

library(rgdal)
library(WDI)
library(dplyr)

indicator <- "SI.POV.GINI"

url <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip"

tmp <- tempdir()

file <- basename(url)

download.file(url, file)

unzip(file, exdir = tmp)

countries <- readOGR(dsn = tmp, 
                     layer = "ne_50m_admin_0_countries", 
                     encoding = "UTF-8",
                     verbose = FALSE)


dat <- WDI(indicator = indicator, 
           start = 1990, 
           end = 2014)

dat[[indicator]] <- round(dat[[indicator]], 1)

datyr <- filter(dat, year == 2013)

dat2 <- dat %>%
  filter(!is.na(SI.POV.GINI)) %>%
  group_by(country) %>%
  filter(year == max(year)) %>%
  ungroup() %>%
  arrange(desc(SI.POV.GINI)) %>%
  mutate(g_rank = row_number())
  

full_dat <- left_join(datyr, dat2, by = "iso2c")

keep_cols <- c(1, 2, 6:8)

full_dat <- full_dat[, keep_cols]

countries2 <- merge(countries, 
                    full_dat, 
                    by.x = "iso_a2", 
                    by.y = "iso2c",                    
                    sort = FALSE)

Leaflet map (note the required installation of the dev branch to get the legend):

# devtools::install_github('rstudio/leaflet@feature/color-legend')

library(leaflet)

pal <- colorQuantile("YlGnBu", NULL, n = 6)

# Get some nice legend labels

quantile_labels <- function(vec, n) {
  qs <- round(quantile(vec, seq(0, 1, 1/n), na.rm = TRUE), 1)
  len <- length(qs) - 1
  qlabs <- c()
  for (i in 1:len) {
    j <- i + 1
    v <- paste0(as.character(qs[i]), "-", as.character(qs[j]))
    qlabs <- c(qlabs, v)
  }
  final_labs <- c(qlabs, "Data unavailable")
  final_labs
}

labs <- quantile_labels(countries2$SI.POV.GINI.y, 6)

popup <- paste0("<strong>", countries2$name, "</strong><br>",
                "<strong>Gini index: </strong>", countries2$SI.POV.GINI.y,  
                      "<br><strong>Global inequality rank: </strong>", countries2$g_rank, 
                      "<br><strong>Year of estimate: </strong>", countries2$year.y)

mb_tiles <- "http://a.tiles.mapbox.com/v3/kwalkertcu.l1fc0hab/{z}/{x}/{y}.png"

mb_attribution <- 'Mapbox <a href="http://mapbox.com/about/maps" target="_blank">Terms &amp; Feedback</a>'

gini_map <- leaflet(data = countries2) %>%
  addTiles(urlTemplate = mb_tiles,  
           attribution = mb_attribution) %>%
  setView(0, 0, zoom = 3) %>%
  addPolygons(fillColor = ~pal(SI.POV.GINI.y), 
              fillOpacity = 0.8, 
              color = "#BDBDC3", 
              weight = 1, 
              popup = popup) %>%
  addLegend(colors = c(RColorBrewer::brewer.pal(6, "YlGnBu"), "#808080"),  
            bins = 6, 
            position = 'bottomright', 
            title = "Gini index", 
            labels = labs)

gini_map