我试图在地图上绘制一个简单的多边形来表示我感兴趣的区域。迄今为止,我已将多边形定义为并且能够单独绘制它。
poly <- st_polygon(list(as.matrix(data.frame(lat = c(40, 40, 60, 60, 40),
lon = c(-60, -40, -40, -60, -60))))) #EDIT: these are lat/lon coordinates
ggplot() +
geom_sf(data = poly,
aes(geometry = geometry),
col = "red)
但是,我需要添加底图,以便可以显示该多边形相对于其他空间元素的位置。
# Attempt #1: stamenmaps basemap
grid_box <- c(left = -70,
right = -30,
bottom = 35,
top = 70)
base <- get_stamenmap(grid_box, zoom = 6, maptype = "terrain-background", color = "color")
ggmap(base) +
geom_sf(data = poly,
aes(geometry = geometry),
col = "red")
Coordinate system already present. Adding new coordinate system, which will replace the existing one.
Error in `geom_sf()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 4th layer.
Caused by error in `FUN()`:
! object 'lon' not found
Run `rlang::last_error()` to see where the error occurred.
我发现向多边形添加 crs 的唯一方法如下(st_transform 和 st_as_sf 不起作用),但这极大地改变了坐标的比例,并且仍然无法绘制。
# Attempt #2: new CRS
poly <- st_polygon(list(as.matrix(data.frame(lon = c(-62, -43, -43, -62, -62),
lat = c(43, 43, 70, 70, 43))))) %>%
st_sfc(crs = 3857)
ggmap(base) +
geom_sf(data = poly,
aes(geometry = geometry),
col = "red")
Coordinate system already present. Adding new coordinate system, which will replace the existing one.
Error in `geom_sf()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 4th layer.
Caused by error in `FUN()`:
! object 'lon' not found
Run `rlang::last_error()` to see where the error occurred.
How can I get this polygon to plot over my basemaps?