2

I want to create a world street map using Rstudio. I have this code:

countries_map <-map_data("world")
world_map<-ggplot() + 
  geom_map(data = countries_map, 
           map = countries_map,aes(x = long, y = lat, map_id = region, group = group),
           fill = "light blue", color = "black", size = 0.1)

the problem: I want to see the names of the countries and to see the map like this one:

enter image description here

Thanks for your help!

www
  • 38,575
  • 12
  • 48
  • 84

1 Answers1

2

We can use the leaflet package. See this link to learn the choice of base map (https://leaflet-extras.github.io/leaflet-providers/preview/). Here I used the "Esri.WorldStreetMap", which is the same as your example image shows.

library(leaflet)
leaflet() %>%
  addProviderTiles(provider = "Esri.WorldStreetMap") %>%
  setView(0, 0, zoom = 1)

enter image description here

In addition to leaflet, here I further introduced two other packages to create interactive maps, which are tmap and mapview.

library(sf)
library(leaflet)
library(mapview)
library(tmap)

# Gett the World sf data
data("World")

# Turn on the view mode in tmap
tmap_mode("view")

# Plot World using tmap
tm_basemap("Esri.WorldStreetMap") +
tm_shape(World) +
  tm_polygons(col = "continent")

enter image description here

# Plot world using mapview
mapview(World, map.types = "Esri.WorldStreetMap")

enter image description here

Update

Here is a way toa add text to each polygon using the tmap package.

library(sf)
library(leaflet)
library(mapview)
library(tmap)

# Gett the World sf data
data("World")

# Turn on the view mode in tmap
tmap_mode("plot")

# Plot World using tmap
tm_basemap("Esri.WorldStreetMap") +
  tm_shape(World) +
  tm_polygons() +
  tm_text(text = "iso_a3")

enter image description here

If you have to use ggplot2, you can prepare your into data as an sf object and use geom_sf and geom_sf_text as follows.

library(sf)
library(tmap)
library(ggplot2)

# Gett the World sf data
data("World")

ggplot(World) +
  geom_sf() +
  geom_sf_text(aes(label = iso_a3))

enter image description here

www
  • 38,575
  • 12
  • 48
  • 84
  • Thanks for your answer. There is an option to create the map without an interactive map? only to see the map with countries name and etc..? Thank you! – Liran Ben Zion Mar 03 '19 at 01:04
  • @LiranBenZion The base map you asked for is a typical online interactive setting, so what you are asking is confusing. – www Mar 03 '19 at 01:09
  • OK I understand. I will try to explain my self: at my code, I see the world map without names. I try to see the map with names..is it possible? Thanks! – Liran Ben Zion Mar 03 '19 at 01:12
  • @LiranBenZion Glad to help. If my post solves your question, please accept it as the answer. – www Mar 03 '19 at 22:31