我尝试将具有自定义坐标的点添加到地图中。一般来说,这似乎适用于 sf 包提供的 NC 地图。但是,它在我从https://gdz.bkg.bund.de/index.php/default/open-data/verwaltungsgebiete-1-2-500-000-stand-31-12-下载的 shapefile 中不起作用vg2500-12-31.html - 只要我只绘制这张新地图。如果我同时绘制两个形状文件,第二个形状文件上的点会突然出现。如何将自定义点仅添加到德国地图?
library("ggplot")
library("sf")
nc <- st_read(system.file("shape/nc.shp", package="sf"))
ger_shape <- st_read("data/shapefiles/VG2500_LAN.shp")
ger_shape <- ger_shape[which(ger_shape$GF == 4),]
city <- data.frame(name = "1", lat = 35.7721, lng = -78.63861)
city2 <- data.frame(name = c("2","3"), lat = c(53,50), lng = c(10.2,10.2))
ggplot() +
geom_sf(data = nc) +
coord_sf(lims_method = "geometry_bbox") +
geom_point(data = city, aes(x = lng, y = lat), color = 'red')
Run Code Online (Sandbox Code Playgroud)
ggplot() +
geom_sf(data = ger_shape) +
coord_sf(lims_method = "geometry_bbox") +
geom_point(data = city2, aes(x = lng, y = lat), color = 'red')
Run Code Online (Sandbox Code Playgroud)
ggplot() +
geom_sf(data = nc) +
geom_sf(data = ger_shape) +
coord_sf(lims_method = "geometry_bbox") +
geom_point(data = city, aes(x = lng, y = lat), color = 'red') +
geom_point(data = city2, aes(x = lng, y = lat), color = 'red')
Run Code Online (Sandbox Code Playgroud)
问题在于,BKG 提供的德国 shapefile 使用的坐标参考系 (crs) 是 Gauss-Kruger zone 3(选中st_crs(ger_shape)),它没有以度数指定位置,而您以度数指定了城市位置。因此,城市的坐标远离德国,您可以看到正在删除该coord_sf行:
library("ggplot2")
library("sf")
nc <- st_read(system.file("shape/nc.shp", package="sf"))
ger_shape <- st_read("shp/VG250_LAN.shp")
ger_shape <- ger_shape[which(ger_shape$GF == 4),]
city2 <- data.frame(name = c("2","3"), lat = c(53,50), lng = c(10.2,10.2))
ggplot() +
geom_sf(data = ger_shape) +
geom_point(data = city2, aes(x = lng, y = lat), color = 'red')
Run Code Online (Sandbox Code Playgroud)
因此,要将您的点添加到德国地图上,您必须在形状文件的 crs 中指定坐标,或将 crs 转换为例如WGS84使用st_transform以度为单位指定位置的坐标:
ger_shape <- sf::st_transform(ger_shape, "WGS84")
ggplot() +
geom_sf(data = ger_shape) +
coord_sf(lims_method = "geometry_bbox", crs = st_crs(ger_shape)) +
geom_point(data = city2, aes(x = lng, y = lat), color = 'red')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
387 次 |
| 最近记录: |