ggmap/sf 是否仍然在错误的位置绘制点?

问题描述 投票:0回答:1

这个老错误仍然尚未解决,还是有合理的解决方法?

部分城市:

require(ggplot2)
require(ggmap)
require(sf)

cities = sf::read_sf('{
  "type": "FeatureCollection",
  "name": "cities",
  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
  },
  "features": [{
      "type": "Feature",
      "properties": {
        "city": "London"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-0.13, 51.51]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "city": "Rome"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [12.48, 41.89]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "city": "Stockholm"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [18.07, 59.33]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "city": "Istanbul"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [28.96, 41.01]
      }
    }
  ]
}
')

还有 ggmap 底图:

bb = c(left = -11, bottom = 35, right = 42, top = 65)
europe = ggmap::get_stamenmap(bbox = bb, maptype = 'toner-lite', zoom = 3)

ggmap(europe) + 
  geom_sf(data = cities, inherit.aes = FALSE, col = 'red', size = 3)

r ggplot2 r-sf ggmap
1个回答
0
投票

我认为问题在于

ggmap
使用坐标对象
CoordMap
来决定栅格像素的放置位置。通过切换到
coord_sf
(当您添加
geom_sf
图层时会发生这种情况),您将导致栅格不对齐。如果您提取点的坐标,您可以简单地将它们叠加为
geom_point
层:

ggmap(europe) + 
  geom_point(data = as.data.frame(st_coordinates(cities)), aes(X, Y), 
             inherit.aes = FALSE, col = 'red', size = 3)

© www.soinside.com 2019 - 2024. All rights reserved.