Arihant
Arihant

Reputation: 589

R: rworldmap map issue and leaflet application

I have made a leaflet map to overlay some countries on a R leaflet webmap, however there seems to be something wrong with the projection/layout and part of Russia finds it way onto the adjacent frame. enter image description here

The code:

library(rworldmap)
library(countrycode)



## country is in text 
cnt <- c("Russia","Afghanistan", "Albania"," Algeria"," Argentina","   Armenia", "Azerbaijan"," Bangladesh"," Belarus")


#convert to ISO3 code
iso3 = countrycode(cnt, "country.name", "iso3c")

df= as.data.frame(cnt)
malMap <- joinCountryData2Map(df, joinCode = "ISO3", nameJoinColumn = "cnt")

## subset data
dfapr <- malMap[malMap$ISO3 %in% iso3, ]
plot(dfapr)

The main consequence of this problem is that if you are making a web-map it would look like this:enter image description here

How do we fix this problem. Is any any low resolution world map, wherein we can select the countries based on ISO code and is geometrically more consistent.

Upvotes: 4

Views: 1847

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78832

Here's an example of what I meant by the comment.

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

# make sure to use the latest maps package
# it was recently updated at the time of the answer

world <- map("world", fill=TRUE, plot=FALSE)
world_map <- map2SpatialPolygons(world, sub(":.*$", "", world$names))
world_map <- SpatialPolygonsDataFrame(world_map,
                                      data.frame(country=names(world_map), 
                                                 stringsAsFactors=FALSE), 
                                      FALSE)

cnt <- c("Russia", "Afghanistan", "Albania", "Algeria", "Argentina", "Armenia",
         "Azerbaijan", "Bangladesh", "Belarus")

target <- subset(world_map, country %in% cnt)

leaflet(target) %>% 
  addTiles() %>% 
  addPolygons(weight=1)

enter image description here

Upvotes: 7

Related Questions