这个问题与how map certain USDA hardiness zones in R有关,但我只需要一张CA,NM和AZ的 map .我应该在filter命令中输入什么才能只包含这3个州?

library(sf)
library(tidyverse)
library(USAboundaries)

state_zip <- "https://www.weather.gov/source/gis/Shapefiles/County/s_22mr22.zip"
 download.file(state_zip, destfile = ".", junkpaths=TRUE, overwrite=TRUE)
 
 unzip("state_zip.zip", junkpaths = TRUE, exdir = ".")

 state_boundaries <- read_sf(".s_22mr22.shp")

temp_shapefile <- tempfile()
download.file('http://prism.oregonstate.edu/projects/public/phm/phm_us_shp.zip', temp_shapefile)
unzip(temp_shapefile)

# Read full shapefile
shp_hardness <- read_sf('phm_us_shp.shp')

 shp_hardness_subset <- shp_hardness %>%
   filter(str_detect(ZONE, '9b|10a|10b|6a|6b|7a|7b'))

我想要的输出是这张 map ,用坚硬地带着色

 ca.az.nm <- subset(state_boundaries, STATE=="CA" | STATE=="AZ" |  STATE=="NM")
 
 ggplot() +
   geom_sf(data =  ca.az.nm) 

enter image description here

 ggplot() +
   geom_sf(data =  ca.az.nm) + 
   geom_sf(data =  shp_hardness_subset, aes(fill = ZONE))

enter image description here

推荐答案

当您使用sf时,可用的工具之一是st_filter(),这是一种空间过滤器,允许您 Select 相交的几何图形.尽管根据您的需要,这一点可能就足够了,因为相交的坚硬多边形会延伸很远.在下面的示例中,还包括ca.az.nm个多边形的相交和ca.az.nm个边界框的裁剪.

library(sf)
library(dplyr)


url_lst = list(state_zip = "https://www.weather.gov/source/gis/Shapefiles/County/s_22mr22.zip",
               phm_zip = 'http://prism.oregonstate.edu/projects/public/phm/phm_us_shp.zip')

tempd <- tempdir()

zip_lst <- lapply(url_lst, \(url){
  destfile <- file.path(tempdir(),basename(url))
  if(!file.exists(destfile)) download.file(url, destfile = destfile, mode = "wb", overwrite=TRUE)
  destfile
})

state_boundaries <- read_sf(paste0("/vsizip/",zip_lst$state_zip))
shp_hardness <- read_sf(paste0("/vsizip/",zip_lst$phm_zip))

# check crs
st_crs(state_boundaries) == st_crs(shp_hardness)
#> [1] TRUE

ca.az.nm <- state_boundaries %>% filter(STATE %in% c("CA", "AZ", "NM"))

shp_hardness_subset <- shp_hardness %>%
  filter(str_detect(ZONE, '9b|10a|10b|6a|6b|7a|7b')) %>% 
  # sf complains about few geometries in phm_us_shp.zip 
  st_make_valid() %>% 
  # spatial filter by ca.az.nm, default predicate is "intersects"
  st_filter(ca.az.nm)

p1 <- ggplot() +
  geom_sf(data =  ca.az.nm) + 
  geom_sf(data =  shp_hardness_subset, aes(fill = ZONE)) +
  theme_bw()

# intersection by ca.az.nm:
shp_hardness_subset_clip <- st_intersection(shp_hardness_subset, ca.az.nm)
p2 <- ggplot() +
  geom_sf(data =  ca.az.nm) + 
  geom_sf(data =  shp_hardness_subset_clip, aes(fill = ZONE)) +
  theme_bw()

# crop by bounding box of ca.az.nm:
shp_hardness_subset_crop <- st_crop(shp_hardness_subset, ca.az.nm) 
p3 <- ggplot() +
  geom_sf(data =  ca.az.nm) + 
  geom_sf(data =  shp_hardness_subset_crop, aes(fill = ZONE)) +
  theme_bw() 

patchwork::wrap_plots(p1, p2, p3, ncol = 1, guides = "collect")

创建于2023-05-11年第reprex v2.0.2

R相关问答推荐

如何使用ggplot重新绘制LASO回归图?

大规模重新标记haven标签数据

多重RHS固定估计

在"gt"表中添加第二个"groupname_col",而不连接列值

使用geom_segment()对y轴排序

将小数分隔符放在R中的前两位数字之后

将多列合并为单独的名称—值对

Select 季度月值

使用for循环和粘贴创建多个变量

R中1到n_1,2到n_2,…,n到n_n的所有组合都是列表中的向量?

为什么我使用geom_density的绘图不能到达x轴?

Data.table';S GForce-将多个函数应用于多列(带可选参数)

如何在R中通过多个变量创建交叉表?

KM估计的差异:SvyKm与带权重的调查

从R中发出的咕噜声中的BUG?

R中Gamma回归模型均方误差的两种计算方法不一致

提高圣彼得堡模拟的速度

删除字符串R中的重复项

有没有办法将勾选/审查标记添加到R中的累积关联图中?

如何在R中添加标识连续日期的新列