gpt4 book ai didi

r - geom_sf 映射点/形状

转载 作者:行者123 更新时间:2023-12-01 08:21:26 25 4
gpt4 key购买 nike

我正在使用 Tigris 包下载带有代码的形状文件,

options(tigris_class = "SF") 

虽然我可以轻松地映射多边形
ggplot(data=zctas) +
geom_sf(aes())

我正在努力创建一个标记/点而不是填充的多边形(例如,一个形状——就像 zctas 里面的一个圆圈),因为我从底格里斯河拉下来的形状文件没有 x 的纬度/经度和 y AES 映射。形状文件有一个“几何”列,我想知道我是否可以使用它?

此处的文档, https://ggplot2.tidyverse.org/reference/ggsf.html

似乎表明geom_sf可以使用go create points? (我在猜测使用 zctas 多边形的质心?)但我找不到一个例子?
感谢任何资源来识别从这个 Tigris 生成的形状文件中映射点的代码,和/或提示和/或替代方法。

最佳答案

您正在寻找 stat_sf_coordinates() ,描述为 here.

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()



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



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



reprex package (v0.3.0) 于 2019-11-03 创建

关于最后一个示例的 geometry = geometry 调用中的 aes() 语句的一条评论:如果在数据集中找到几何列, geom_sf()stat_sf_coordinates() 都会自动映射几何列。但是, geom_point() 不知道几何列,因此不会自动映射,因此我们必须手动映射。

关于r - geom_sf 映射点/形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58676661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com