向 sf 地图添加点时 ggplot 的不一致行为

Inconsistent behaviour of ggplot when adding points to sf maps

使用 ggplot 我想向 sf 地图添加点。拿这个代码:

ggplot(data = shapefile) +
  geom_sf()+
  geom_point(x = -70.67,y =-33.45, size = 10, colour = "red")

此代码适用于我的一个 shapefile 但不适用于另一个,我不确定为什么。这是我的第一个 shapefile 的代码输出:

下面是第二个 shapefile 的代码输出:

第二次调用无法识别坐标的潜在原因?我在两个图之间看到的唯一区别是,在第一个图中,经度和纬度用数字注释,在第二个图中,在它们的 north/south 和 east/west 方向之后。

不一致的行为是由于每个 shapefile 的投影不同。您通常需要提供与您正在使用的投影单位相匹配的点位置。您可以将这些点转换为 shapefile 的投影,或者,如果您不关心数据是否在地理坐标系中,您可以转换为 4326,即 WGS84 基准上的 lat/long。

方法 1:维护 shapefile 的投影。此方法会将您的点转换为空间 sf 数据类型,因此您可以使用 geom_sf.

进行绘图
pts <- st_point(c(-70.67, -33.45)) %>% st_sfc() %>% st_as_sf(crs=st_crs(4326))

ggplot(data = shapefile) +
  geom_sf() +
  geom_sf(data = pts, size = 10, colour = "red")

方法 2:将 shapefile 转换为 EPSG 4326。

ggplot(data = shapefile %>% st_transform(st_crs(4326))) +
  geom_sf() +
  geom_point(x = -70.67,y =-33.45, size = 10, colour = "red")