在SF对象下绘制静态底图 [英] Plotting static base map underneath a sf object

查看:45
本文介绍了在SF对象下绘制静态底图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 库(sf)#devtools :: install_github("tidyverse/ggplot2")库(ggplot2)库(ggmap)nc<-st_read(system.file("shape/nc.shp",package ="sf"))nc_map<-get_map(位置=北卡罗来纳州北卡罗来纳州",缩放= 7)ggmap(nc_map)nc_centers<-st_centroid(nc)nc_centers%>%ggplot()+geom_sf(aes(color = SID79,size = BIR74),show.legend ="point")+coord_sf(基准= NA)+theme_minimal() 

我也宁愿使用 source ="osm" 作为样式,但是这些总是返回'400 Bad Request'.

也许还有另一个适合基础地图的软件包?

解决方案

您可能考虑重新投影数据,但是以下代码似乎对我有用.
请参阅(v0.2.0)创建于2018-04-03.

I'm trying to plot a static base map underneath my sf object (for print). When using ggmap I first get a lot of errors, then I can't seem to figure out how to chain the base map to my ggplot2 object with a 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()

I also rather use the source = "osm" as style but those will always return '400 Bad Request'.

Is there maybe another good package for base maps?

解决方案

You might consider reprojecting your data but the following code seems to work for me.
See here for an explanation about why you need inherit.aes = FALSE and see here for an alternative solution with base plots.

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.

Created on 2018-04-03 by the reprex package (v0.2.0).

这篇关于在SF对象下绘制静态底图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆