2
votes

I have a hard time to figure out why I can't map the right country on the map. I have gone through all of my codes, I still don't understand why is not working right.

If you see any problems, please let me know. I appreciate.

This is dataset

      Country Total Code
1        China 34620  CHN
2        Japan    89  JPN
3    Singapore    40  SGP
4     Thailand    32  THA
5    Hong Kong    26  HKG
6     S. Korea    24  KOR
7       Taiwan    17  TWN
8     Malaysia    16  MYS
9    Australia    15  AUS
10     Germany    14  DEU
11     Vietnam    13  VNM
12         USA    12  USA
13      France    11  FRA
14       Macao    10  MAC
15      U.A.E.     7  ARE
16      Canada     7  CAN
17 Philippines     3  PHL
18       India     3  IND
19       Italy     3  ITA
20        U.K.     3  GBR
21      Russia     2  RUS
22     Finland     1  FIN
23   Sri Lanka     1  LKA
24      Sweden     1  SWE
25       Nepal     1  NPL
26    Cambodia     1  KHM
27       Spain     1  ESP
28     Belgium     1  BEL

library(leaflet)
library(maps)
library(maptools)

case <- read.csv("Cases_02072020_v1.csv",stringsAsFactors = FALSE)
Country = map("world", fill = TRUE, plot = FALSE, regions=iso.expand(case$Code,regex = TRUE))

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


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

case$labels <- sprintf(
  "<strong>Country:%s</strong><br/>Total:%g",
  case$Country, case$Total)%>% lapply(htmltools::HTML) 


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



1
I am not an expert with leaflet but are you not plotting the whole world with your call Country = map("world"... and leaflet(Country)... - MarBlo
I already got it, first of fall the names of country are not matching with map(), the second we need to order the data - BIN

1 Answers

2
votes

You need to repeat each row of your case data frame so that the countries match each individual polygon on the map. This means ensuring you order them correctly and also you need to incorporate Macao and Hong Kong into China (or change the way you split IDs to handle them there).

Here is a full working version:

library(leaflet)
library(maps)
library(maptools)

case <- structure(list(Country = c("China", "Japan", "Singapore", "Thailand",
"Hong Kong", "S. Korea", "Taiwan", "Malaysia", "Australia", "Germany",
"Vietnam", "USA", "France", "Macao", "U.A.E.", "Canada", "Philippines",
"India", "Italy", "U.K.", "Russia", "Finland", "Sri Lanka", "Sweden",
"Nepal", "Cambodia", "Spain", "Belgium"), Total = c(34620, 89,
40, 32, 26, 24, 17, 16, 15, 14, 13, 12, 11, 10, 7, 7, 3, 3, 3,
3, 2, 1, 1, 1, 1, 1, 1, 1), Code = c("CHN", "JPN", "SGP", "THA",
"HKG", "KOR", "TWN", "MYS", "AUS", "DEU", "VNM", "USA", "FRA",
"MAC", "ARE", "CAN", "PHL", "IND", "ITA", "GBR", "RUS", "FIN",
"LKA", "SWE", "NPL", "KHM", "ESP", "BEL")), row.names = c(NA,
-28L), class = "data.frame")



case <- case[order(case$Country), ]
Country = map("world", fill = TRUE, plot = FALSE, regions = iso.expand(case$Code,regex = F))
IDs <- Country$names
Country <- map2SpatialPolygons(Country,
                               IDs=IDs,
                               proj4string=CRS("+proj=longlat +datum=WGS84"))

case[nrow(case) + 1, ] <- case[case$Code == "ESP",]
case$Country[nrow(case)] <- "Canary Islands"
case$Country[case$Country == "S. Korea"] <- "South Korea"
case$Country[case$Country == "U.K."] <- "UK"
case$Country[case$Country == "U.A.E."] <- "United Arab Emirates"
case$Total[case$Country == "China"] <- case$Total[case$Country == "China"] +
                                       case$Total[case$Country == "Hong Kong"] +
                                       case$Total[case$Country == "Macao"]

case <- case[-which(case$Country == "Hong Kong"), ]
case <- case[-which(case$Country == "Macao"), ]
case <- case[order(case$Country), ]

reps <- as.numeric(table(sapply(strsplit(IDs, ":"), function(x) x[1])))
case <- do.call(rbind, mapply(function(x, y){ x[rep(1,y),]},
                              split(case, case$Country),
                              reps,
                              SIMPLIFY = F))
pal <- colorNumeric(
  palette = "Blues",
  domain = as.numeric(case$Total))


case$labels <- sprintf(
  "<strong>Country:%s</strong><br/>Total:%g",
  case$Country, case$Total)%>% lapply(htmltools::HTML)


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

enter image description here

Obviously this is just a snapshot, but you can see China is coloured correctly.