4

I am trying to plot few countries using leaflet but the countries that it is showing is incorrect. Following is my minimal reproducible example:

library(leaflet)
library(maps)


  df <- data.frame(name = c("Afghanistan", "Albania" , "Algeria" , "Armenia"),
                   code = c("AFG", "ALB", "DZA", "ARM"),
                    val = c(5, 10, 15, 20), stringsAsFactors = FALSE)

  pal <- colorNumeric(
    palette = "Blues",
    domain = as.numeric(df$val))

  labels <- sprintf(
    "<strong>Country:%s</strong><br/>Value:%g /",
    df$name, df$val)%>% lapply(htmltools::HTML) 

  Country = map("world", fill = TRUE, plot = FALSE, regions=iso.expand(df$code,regex = TRUE))

  leaflet(Country) %>% addTiles() %>%
    addPolygons(fillOpacity = 0.6,  smoothFactor = 0.5, stroke = TRUE, weight = 1, 
                color = ~pal(as.numeric(df$val)),
                label = labels)

I get the following leaflet with this:enter image description here

As you can see Algeria is shown as Albania. If I remove the Armenia from my data and plot the leaflet I get correct location. Following is the code and image for that.

library(leaflet)
  library(maps)

  df <- data.frame(name = c("Afghanistan", "Albania" , "Algeria" ),
                   code = c("AFG", "ALB", "DZA"),
                    val = c(5, 10, 15), stringsAsFactors = FALSE)

  pal <- colorNumeric(
    palette = "Blues",
    domain = as.numeric(df$val))

  labels <- sprintf(
    "<strong>Country:%s</strong><br/>Value:%g /",
    df$name, df$val)%>% lapply(htmltools::HTML) 

  Country = map("world", fill = TRUE, plot = FALSE, regions=iso.expand(df$code,regex = TRUE))

  leaflet(Country) %>% addTiles() %>%
    addPolygons(fillOpacity = 0.6,  smoothFactor = 0.5, stroke = TRUE, weight = 1, 
                color = ~pal(as.numeric(df$val)),
                label = labels)

enter image description here

Am I missing something?

SBista
  • 7,479
  • 1
  • 27
  • 58

1 Answers1

2

I think the issue is related to the polygons order in map object (if you set label = Country$names, the labels are now correct). Anyway, you can solve the problem by converting Country into a SpatialPolygons object (see here).

library(maptools)
IDs <- sapply(strsplit(Country$names, ":"), function(x) x[1])
Country <- map2SpatialPolygons(Country, 
                               IDs=IDs, 
                               proj4string=CRS("+proj=longlat +datum=WGS84"))

leaflet(Country) %>% addTiles() %>%
  addPolygons(fillOpacity = 0.6,  smoothFactor = 0.5, stroke = TRUE, weight = 1, 
              color = pal(as.numeric(df$val)),
              label = labels)

enter image description here

DJack
  • 4,850
  • 3
  • 21
  • 45
  • Exactly what I needed! – SBista Apr 12 '18 at 16:19
  • It solved the problem in this case but in my actual code where there are more countries the problem still persists. Any pointers on how to solve that? – SBista Apr 12 '18 at 16:41
  • You could merge `pal(as.numeric(df$val)` and `labels` with `Country` using a `SpatialPolygonsDataframe` and use `Country$val` and `Country$label` in the `addPolygons` function. Like this, `val` and `labels` should be associated with the correct country. – DJack Apr 12 '18 at 17:07