how to create a world street map with r?

て烟熏妆下的殇ゞ 提交于 2019-12-10 23:36:28

问题


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:

Thanks for your help!


回答1:


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)

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")

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

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")

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))



来源:https://stackoverflow.com/questions/54964279/how-to-create-a-world-street-map-with-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!