在sf对象下绘制静态基本映射

Tde*_*eus 9 gis r geospatial ggmap r-sf

我正在尝试在我的sf对象下面绘制一个静态基本地图(用于打印).当ggmap我使用时,我首先得到很多错误,然后我似乎无法弄清楚如何将基本地图链接到我的ggplot2对象geom_sf.

library(sf)
# devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
library(ggmap) 

nc <- st_read(system.file("shape/nc.shp", package="sf"))
nc_map <- get_map(location = "North Carolina, NC", zoom = 7)

ggmap(nc_map)

nc_centers <- st_centroid(nc)

nc_centers %>%
  ggplot() +
  geom_sf(aes(color = SID79, size = BIR74),
          show.legend = "point") +
  coord_sf(datum = NA) +
  theme_minimal()
Run Code Online (Sandbox Code Playgroud)

我也更喜欢使用source = "osm"as样式,但总是会返回'400 Bad Request'.

基地图可能还有另一个好包吗?

Chr*_*ris 13

我最近一直在开发一个,有人可能会觉得有用。ggmap 答案现在需要一个 API 密钥才能与谷歌地图一起使用,这增加了一些额外的功能。

basemapR还允许您通过使用边界框更灵活地设置底图的范围。

#devtools::install_github('Chrisjb/basemapR')
library(basemapR)
library(sf)
library(ggplot2)


nc <- st_read(system.file("shape/nc.shp", package="sf"))

nc_centers <- st_centroid(nc)

# create bbox from our nc layer and expand it to include more area above/below
bbox <- expand_bbox(st_bbox(nc_centers), X = 0, Y = 150000)

ggplot() +
  base_map(bbox, increase_zoom = 2, basemap = 'google-terrain') +
  geom_sf(data = nc_centers, 
          aes(color = SID79, size = BIR74),
          show.legend = "point", inherit.aes = FALSE) +
  coord_sf(datum = NA,
           xlim = c(bbox['xmin'], bbox['xmax']),
           ylim = c(bbox['ymin'], bbox['ymax'])) +
  theme_minimal() +
  labs(caption = 'map data \uA9 2020 Google')
Run Code Online (Sandbox Code Playgroud)

请记住在标题或地图中的其他位置引用谷歌地图

在此输入图像描述


Gil*_*les 8

您可以考虑重新投影您的数据,但以下代码似乎对我有用.
请参阅此处以获取有关您需要的原因的说明,inherit.aes = FALSE在此处查看基础图的替代解决方案.

library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.1.3, proj.4 4.9.2
# devtools::install_github("r-lib/rlang")
library(ggplot2)
library(ggmap) 

nc <- st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `/home/gilles/R/x86_64-pc-linux-gnu-library/3.4/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs
nc_map <- get_map(location = "North Carolina, NC", zoom = 7)
#> Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=North+Carolina,+NC&zoom=7&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
#> Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=North%20Carolina,%20NC&sensor=false
nc_centers <- st_centroid(nc)
#> Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
#> of_largest_polygon): st_centroid does not give correct centroids for
#> longitude/latitude data

ggmap(nc_map) +
    geom_sf(data = nc_centers, 
            aes(color = SID79, size = BIR74),
            show.legend = "point", inherit.aes = FALSE) +
    coord_sf(datum = NA) +
    theme_minimal()
#> Coordinate system already present. Adding new coordinate system, which will replace the existing one.
Run Code Online (Sandbox Code Playgroud)

reprex包(v0.2.0)于2018-04-03创建.


mik*_*eck 6

您还可以使用ggspatial提供“地图瓦片”注释层的包。

ggplot(nc_centers) +
    annotation_map_tile(zoom = 7) +
    geom_sf(aes(color = SID79, size = BIR74),
            show.legend = "point", inherit.aes = FALSE) +
    coord_sf(datum = NA) +
    theme_minimal()
Run Code Online (Sandbox Code Playgroud)