使用 R 和 geojson 数据将名称添加到地图上的 x、y 点
Adding names to x,y points on maps using R and geojson data
我想轻松地为尼泊尔的地区添加名称。大多数在线答案都使用第二组数据并将地图连接到数据,然后使用它来命名点。我没有数据。我只有一个带有名称列、纬度、经度等的 geojson。
nepal_data <- geojson_read("https://raw.githubusercontent.com/mesaugat/geoJSON-Nepal/master/nepal-districts.geojson", what = "sp")
nepal_fort <- tidy(nepal_data)
nepal_plot <- ggplot() +
geom_polygon(data = nepal_fort,
aes(x = long, y = lat, group = group),
fill="blue",
color="white") +
coord_map()
我不知道如何给地图添加名字。这个问题在某种程度上与 有关,但没有 csv 数据部分。
当我按原样 运行 代码时,绘图已生成,但我也得到:
Unequal factor levels: coercing to characterbinding character and factor vector, coercing into character vectorbinding character and factor vector, coercing into character vectorbinding character and factor.....
我认为使用 sf
-package
时您想要的很简单
library( sf )
library( ggplot2 )
nepal_data <- sf::st_read("https://raw.githubusercontent.com/mesaugat/geoJSON-Nepal/master/nepal-districts.geojson")
ggplot() +
geom_sf( data = nepal_data, fill = "blue", colour = "white" ) +
geom_sf_label( data = nepal_data, aes( label = DISTRICT ), size = 2 )
我想轻松地为尼泊尔的地区添加名称。大多数在线答案都使用第二组数据并将地图连接到数据,然后使用它来命名点。我没有数据。我只有一个带有名称列、纬度、经度等的 geojson。
nepal_data <- geojson_read("https://raw.githubusercontent.com/mesaugat/geoJSON-Nepal/master/nepal-districts.geojson", what = "sp")
nepal_fort <- tidy(nepal_data)
nepal_plot <- ggplot() +
geom_polygon(data = nepal_fort,
aes(x = long, y = lat, group = group),
fill="blue",
color="white") +
coord_map()
我不知道如何给地图添加名字。这个问题在某种程度上与
当我按原样 运行 代码时,绘图已生成,但我也得到:
Unequal factor levels: coercing to characterbinding character and factor vector, coercing into character vectorbinding character and factor vector, coercing into character vectorbinding character and factor.....
我认为使用 sf
-package
library( sf )
library( ggplot2 )
nepal_data <- sf::st_read("https://raw.githubusercontent.com/mesaugat/geoJSON-Nepal/master/nepal-districts.geojson")
ggplot() +
geom_sf( data = nepal_data, fill = "blue", colour = "white" ) +
geom_sf_label( data = nepal_data, aes( label = DISTRICT ), size = 2 )