簡體   English   中英

如何用r創建世界街道地圖?

[英]how to create a world street map with r?

我想使用 Rstudio 創建世界街道地圖。 我有這個代碼:

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)

問題:我想查看國家名稱並查看這樣的地圖:

在此處輸入圖片說明

謝謝你的幫助!

我們可以使用leaflet包。 請參閱此鏈接以了解底圖的選擇( https://leaflet-extras.github.io/leaflet-providers/preview/ )。 在這里,我使用了“Esri.WorldStreetMap”,它與您的示例圖像顯示的相同。

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

在此處輸入圖片說明

除了leaflet ,這里我進一步介紹了另外兩個包來創建交互式地圖,分別是tmapmapview

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

在此處輸入圖片說明

更新

這是使用tmap包向每個多邊形添加文本的方法。

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

在此處輸入圖片說明

如果必須使用ggplot2 ,則可以將數據准備為sf對象並使用geom_sfgeom_sf_text如下。

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

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

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM