4

I made this plot

enter image description here

With the following code.

library(XML)
library(ggplot2)
library(scales)
library(plyr)
library(maps)

unemp <-
  readHTMLTable('http://www.bls.gov/web/laus/laumstrk.htm',
                colClasses = c('character', 'character', 'numeric'))[[2]]

names(unemp) <- c('rank', 'region', 'rate')
unemp$region <- tolower(unemp$region)

us_state_map <- map_data('state')
map_data <- merge(unemp, us_state_map, by = 'region')

map_data <- arrange(map_data, order)

states <- data.frame(state.center, state.abb)

p1 <- ggplot(data = map_data, aes(x = long, y = lat, group = group))
p1 <- p1 + geom_polygon(aes(fill = cut_number(rate, 5)))
p1 <- p1 + geom_path(colour = 'gray', linestyle = 2)
p1 <- p1 + scale_fill_brewer('Unemployment Rate (Jan 2011)', palette  = 'Set1')
p1 <- p1 + coord_map()
p1 <- p1 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)
p1 <- p1 + theme_bw()
p1

Now I want to reproduce the same plot with leaflet R package.

library(leaflet)

leaflet(data = map_data) %>% 
  setView(lng = -77.0167, lat = 38.8833, zoom = 4) %>% 
  addTiles()

How can I annotate rate from map_data data.frame on the map with leaflet as geom_polygon did in ggplot2 version?

halfer
  • 19,824
  • 17
  • 99
  • 186
MYaseen208
  • 22,666
  • 37
  • 165
  • 309

1 Answers1

3

Maybe here's one way as a starting point:

mapStates = map("state", fill = TRUE, plot = FALSE)
rates <- cut_number(unemp$rate[match(sub("(.*?):.*", "\\1", mapStates$names), unemp$region)], 5) 

leaflet(data = mapStates) %>% addTiles() %>%
      addPolygons(fillColor = brewer_pal(palette = "Set1")(8)[as.numeric(rates)], stroke = FALSE) %>%
      addLegend(colors = brewer_pal(palette = "Set1")(nlevels(rates)), labels = levels(rates), opacity = .2) 

enter image description here


Add:

With regards to your other question:

library(raster)
pakistan.adm2.spdf <- getData("GADM", country = "Pakistan", level = 2)
rates <- cut_number(unemployment.df$unemployment[match(pakistan.adm2.spdf@data$NAME_2, unemployment.df$id)], 5)
leaflet(pakistan.adm2.spdf) %>% addTiles() %>% 
  addPolygons(fillColor = brewer_pal(palette = "PuRd")(nlevels(rates))[as.numeric(rates)], stroke = FALSE, fillOpacity = .6) %>% 
  addLegend(colors = brewer_pal(palette = "PuRd")(nlevels(rates)), labels = levels(rates), opacity = .6) %>%
  setView(lng = 69.374268, lat = 30.028617, zoom = 5)

enter image description here

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Thanks @lukeA for your nice answer. Would appreciate if you explain what is the purpose of `(8)[as.numeric(rates)]` is. Thanks – MYaseen208 Jun 28 '15 at 10:56
  • `brewer_pal(palette = "Set1")(8)` creates 8 colors from bewer palette Set1. `as.numeric(rates)` turns the factor levels (here: intervals) into numeric values, like `cut_number(..., labels = FALSE)` would do, too. The brackets `[]` map the 5 numeric interval values to the 8 colors. – lukeA Jun 28 '15 at 11:16
  • Thanks @lukeA for your help. I'm little confuse with the code. Could you help me to translate this code for [this](http://stackoverflow.com/a/17732644/707145). Thanks – MYaseen208 Jun 28 '15 at 11:20
  • Actually I could no figure out how to use `map` command for my actual data. – MYaseen208 Jun 28 '15 at 11:29
  • See my edit - you might wanna add a stroke too `addPolygons`, e.g. `stroke = TRUE, weight = 1, color = "black"`, to make it clearer. – lukeA Jun 28 '15 at 12:28