【发布时间】:2020-05-20 18:38:36
【问题描述】:
有没有人能够在 R 中创建一些美国农业部抗寒区的地图,也许是 ggplot2 and sf packages?我特别想创建一个只有 9b 区和更高颜色 的地图。
我认为创建地图的一些数据可以在这里找到Prism Climate Group,但我缺乏经验,不知道如何处理 GIS 数据(文件扩展名 SGML、XML、DBF、PRJ、SHP、SHX )。
【问题讨论】:
有没有人能够在 R 中创建一些美国农业部抗寒区的地图,也许是 ggplot2 and sf packages?我特别想创建一个只有 9b 区和更高颜色 的地图。
我认为创建地图的一些数据可以在这里找到Prism Climate Group,但我缺乏经验,不知道如何处理 GIS 数据(文件扩展名 SGML、XML、DBF、PRJ、SHP、SHX )。
【问题讨论】:
详细说明@niloc 的答案:
在 Albers 圆锥投影中显示的美国看起来更自然(加拿大边界略微弯曲 - 与原始图像一样)。
这可以通过在您的 {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
【讨论】:
geom_polygon 和geom_sf 不必要地混合在一起似乎也更干净。
在该地图中发生了很多事情,所有插图,带有 F 和 C 的图例,显示在 CONUS 上的状态。最好缩小你的问题。
但这是一个开始。 shapefile 由许多文件(XML、DBF 等)组成,但您只需将read_sf() 指向.shp 文件。可以像使用 data.frame 一样使用 sf 对象进行子集化。
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
【讨论】: