如何用r创建世界街道地图? [英] how to create a world street map with r?
问题描述
我想使用Rstudio创建世界街道地图.我有以下代码:
countries_map< -map_data("world")world_map< -ggplot()+geom_map(数据= country_map,地图= country_map,aes(x =长,y =纬度,map_id =区域,组=组),填充=浅蓝色",颜色=黑色",尺寸= 0.1)
问题:我想查看国家/地区的名称并查看像这样的地图:
感谢您的帮助!
我们可以使用 leaflet
包.请参阅此链接以了解基本地图的选择(
除了 leaflet
,在这里我还介绍了另外两个用于创建交互式地图的包,它们是 tmap
和 mapview
.
库(sf)图书馆(传单)图书馆(mapview)图书馆(tmap)#获取世界sf数据数据(世界")#在tmap中打开视图模式tmap_mode("view")#使用tmap绘制世界tm_basemap("Esri.WorldStreetMap")+tm_shape(世界)+tm_polygons(col ="continent")
#使用mapview绘制世界mapview(World,map.types ="Esri.WorldStreetMap")
更新
这是一种使用 tmap
包向每个多边形添加文本的方法.
库(sf)图书馆(传单)图书馆(mapview)图书馆(tmap)#获取世界sf数据数据(世界")#在tmap中打开视图模式tmap_mode(绘图")#使用tmap绘制世界tm_basemap("Esri.WorldStreetMap")+tm_shape(世界)+tm_polygons()+tm_text(text ="iso_a3")
如果必须使用 ggplot2
,则可以将数据准备为 sf
对象,并使用 geom_sf
和 geom_sf_text
如下.
库(sf)图书馆(tmap)库(ggplot2)#获取世界sf数据数据(世界")ggplot(世界)+geom_sf()+geom_sf_text(aes(label = iso_a3))
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!
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))
这篇关于如何用r创建世界街道地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!