如何在 R 中映射某些美国农业部抗寒区
how map certain USDA hardiness zones in R
有没有人能够在 R 中创建美国农业部抗寒区的地图,也许 ggplot2 and sf packages? I'd specifically like to create a map with only zones 9b and higher in color 。
我认为创建地图的一些数据可以在这里找到 Prism Climate Group,但我没有经验,不知道如何处理 GIS 数据(文件扩展名 SGML,XML ,DBF、PRJ、SHP、SHX)。
那张地图有很多内容,所有的插图,带有 F 和 C 的图例,显示在美国大陆上的状态。最好缩小您的问题范围。
但这是一个开始。 shapefile 由许多文件(XML、DBF 等)组成,但您只需将 read_sf()
指向 .shp
文件即可。 sf
对象的子集可以像 data.frame
.
一样完成
library(sf)
library(tidyverse)
# Download and unzip file
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')
# Subset to zones 9b and higher
shp_hardness_subset <- shp_hardness %>%
filter(str_detect(ZONE, '9b|10a|10b|11a|11b'))
# Plot it
ggplot() +
geom_sf(data = shp_hardness_subset, aes(fill = ZONE)) +
geom_polygon(data = map_data("state"), # add states for context
aes(x=long, y=lat,group=group),
color = 'black',
fill = NA) +
theme_void() # remove lat/long grid lines
详细说明@niloc 的回答:
美国在阿尔伯斯圆锥投影中看起来更自然(加拿大边界略微弯曲 - 与原始图像相同)。
这可以通过在 {ggplot2} 调用中使用 coord_sf(crs = 5070)
来实现。
答案的要点(通过ggplot2::geom_sf()
下载、解压缩和绘图)保持不变。
library(sf)
library(tidyverse)
library(USAboundaries)
# Download and unzip file
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')
# Subset to zones 9b and higher
shp_hardness_subset <- shp_hardness %>%
filter(str_detect(ZONE, '9b|10a|10b|11a|11b'))
# state boundaries for context
usa <- us_boundaries(type="state", resolution = "low") %>%
filter(!state_abbr %in% c("PR", "AK", "HI")) # lower 48 only
# Plot it
ggplot() +
geom_sf(data = shp_hardness_subset, aes(fill = ZONE)) +
geom_sf(data = usa, color = 'black', fill = NA) +
coord_sf(crs = 5070) +
theme_void() # remove lat/long grid lines
有没有人能够在 R 中创建美国农业部抗寒区的地图,也许 ggplot2 and sf packages? I'd specifically like to create a map with only zones 9b and higher in color
我认为创建地图的一些数据可以在这里找到 Prism Climate Group,但我没有经验,不知道如何处理 GIS 数据(文件扩展名 SGML,XML ,DBF、PRJ、SHP、SHX)。
那张地图有很多内容,所有的插图,带有 F 和 C 的图例,显示在美国大陆上的状态。最好缩小您的问题范围。
但这是一个开始。 shapefile 由许多文件(XML、DBF 等)组成,但您只需将 read_sf()
指向 .shp
文件即可。 sf
对象的子集可以像 data.frame
.
library(sf)
library(tidyverse)
# Download and unzip file
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')
# Subset to zones 9b and higher
shp_hardness_subset <- shp_hardness %>%
filter(str_detect(ZONE, '9b|10a|10b|11a|11b'))
# Plot it
ggplot() +
geom_sf(data = shp_hardness_subset, aes(fill = ZONE)) +
geom_polygon(data = map_data("state"), # add states for context
aes(x=long, y=lat,group=group),
color = 'black',
fill = NA) +
theme_void() # remove lat/long grid lines
详细说明@niloc 的回答:
美国在阿尔伯斯圆锥投影中看起来更自然(加拿大边界略微弯曲 - 与原始图像相同)。
这可以通过在 {ggplot2} 调用中使用 coord_sf(crs = 5070)
来实现。
答案的要点(通过ggplot2::geom_sf()
下载、解压缩和绘图)保持不变。
library(sf)
library(tidyverse)
library(USAboundaries)
# Download and unzip file
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')
# Subset to zones 9b and higher
shp_hardness_subset <- shp_hardness %>%
filter(str_detect(ZONE, '9b|10a|10b|11a|11b'))
# state boundaries for context
usa <- us_boundaries(type="state", resolution = "low") %>%
filter(!state_abbr %in% c("PR", "AK", "HI")) # lower 48 only
# Plot it
ggplot() +
geom_sf(data = shp_hardness_subset, aes(fill = ZONE)) +
geom_sf(data = usa, color = 'black', fill = NA) +
coord_sf(crs = 5070) +
theme_void() # remove lat/long grid lines