geom_sf mapping points/shapes

Jan*_*neA 2 r geospatial ggplot2 tigris r-sf

I am using the Tigris package to download shape files with the code,

options(tigris_class = "SF") 
Run Code Online (Sandbox Code Playgroud)

While I can easily map polygons

ggplot(data=zctas) +
  geom_sf(aes())
Run Code Online (Sandbox Code Playgroud)

I am struggling to create a marker/point instead of a filled polygon (for example, a shape -- like a circle inside a zctas) as the shape file I've pulled down from tigris does not have a lat/long for the x and y AES mapping. The shape file has a "geometry" column, and I am wondering if I can use that?

The documentation here, https://ggplot2.tidyverse.org/reference/ggsf.html

appears to indicate the geom_sf can be used go create points? (I am guessing using the centroid of the zctas' polygon?) but I haven't been able to find an example? Grateful for any resources to identify code that maps points from this Tigris generated shape-file, and or tips and/or alternative approaches.

gio*_*mai 5

如果我正确理解您想要实现的目标,您可能只需要使用函数将数据转换为点st_centroid()...最基本的,如下所示:

ggplot(data=st_centroid(zctas)) +
  geom_sf()
Run Code Online (Sandbox Code Playgroud)

也许:

ggplot(data=st_centroid(st_geometry(zctas))) +
  geom_sf()
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息和示例,请参阅: https://r-spatial.github.io/sf/articles/sf5.html


Cla*_*lke 5

您正在寻找stat_sf_coordinates()这里描述

library(ggplot2)

nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `/Library/Frameworks/R.framework/Versions/3.6/Resources/library/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

ggplot(nc) +
  geom_sf()
Run Code Online (Sandbox Code Playgroud)

ggplot(nc) +
  geom_sf() +
  stat_sf_coordinates()
#> Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may
#> not give correct results for longitude/latitude data
Run Code Online (Sandbox Code Playgroud)

ggplot(nc) +
  geom_sf() +
  geom_point(
    aes(color = SID74, size = AREA, geometry = geometry),
    stat = "sf_coordinates"
  ) +
  scale_color_viridis_c(option = "C") +
  theme(legend.position = "bottom")
#> Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may
#> not give correct results for longitude/latitude data
Run Code Online (Sandbox Code Playgroud)

reprex软件包(v0.3.0)创建于2019-11-03

在最后一个示例geometry = geometryaes()调用中,有一条关于语句的注释:两者geom_sf()stat_sf_coordinates()如果在数据集中找到几何列,则会自动映射几何列。但是,geom_point()由于不了解几何列,因此不会自动映射,因此我们必须手动进行映射。