您正在寻找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()
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 包(v0.3.0)于 2019-11-03 创建
关于最后一个示例调用中的geometry = geometry
语句的一条评论:如果在数据集中找到几何列,两者都会自动映射几何列。但是,不知道几何列,因此不自动映射,因此我们必须手动映射。aes()
geom_sf()
stat_sf_coordinates()
geom_point()