如何使用 r 中的 sf 和 tmap 包绘制多个状态
How do I plot multiple state using the sf and tmap packages in r
我正在尝试绘制县级地图。我遇到了一个使用 sf
包和 tmap
包的 tutorial。使用?st_read
,展示了如何获取县级数据,以北卡罗来纳州为例,使用如下代码-
nc = st_read(system.file("shape/nc.shp", package="sf"))
我使用上面的代码绘制了 NC 的县级数据,但是我不确定如何获取其他州的数据。我尝试用 sc.shp
或 va.shp
等替换 nc.shp
,但是我收到以下错误 - Error: Fill argument neither colors nor valid variable name(s)
.
如何提取其他州或更可取的“所有”州的数据。我希望能够绘制整个美国的地图、区域地图(北、南、东、西)以及一个区域内的多个州。
这是我创建的地图的一些可重现代码 -
library(tmap)
library(tmaptools)
library(leaflet)
library(tidyverse)
options(scipen = 999)
nc_data <- st_read(system.file("shape/nc.shp", package="sf")) %>%
mutate(Code = case_when(
AREA <= 0.07 ~ "Label 1", TRUE ~ "Label 2"
))
tm_shape(nc_data)+
tm_polygons("Code", id = "NAME", palette = "Blues")
tmap_mode("view")
tmap_last()
同样,我如何为其他州和多个州绘制此图?
'nc' 数据作为示例数据包含在 sf
包中,因此您必须以其他方式获取其他州的数据。
有几种方法可以绘制县级和州级 shapefile。 urbnmapr(https://github.com/UrbanInstitute/urbnmapr)就是我下面用的那个
library(tidyverse)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(urbnmapr)
library(tmap)
us_counties <- get_urbn_map(map = 'counties', sf = TRUE)
us_counties <- us_counties %>%
mutate(area_m_sq = st_area(.))
tmap_mode('plot')
#> tmap mode set to plotting
tm_shape(us_counties) +
tm_polygons('area_m_sq', palette = 'Blues')
由 reprex package (v1.0.0)
于 2021-02-19 创建
urbnmapr 将 HI 和 AK 移动到美国西部以南的通常插入点。如果这是一个问题,你可能会更好地从底格里斯包 https://github.com/walkerke/tigris.
获取数据
我正在尝试绘制县级地图。我遇到了一个使用 sf
包和 tmap
包的 tutorial。使用?st_read
,展示了如何获取县级数据,以北卡罗来纳州为例,使用如下代码-
nc = st_read(system.file("shape/nc.shp", package="sf"))
我使用上面的代码绘制了 NC 的县级数据,但是我不确定如何获取其他州的数据。我尝试用 sc.shp
或 va.shp
等替换 nc.shp
,但是我收到以下错误 - Error: Fill argument neither colors nor valid variable name(s)
.
如何提取其他州或更可取的“所有”州的数据。我希望能够绘制整个美国的地图、区域地图(北、南、东、西)以及一个区域内的多个州。
这是我创建的地图的一些可重现代码 -
library(tmap)
library(tmaptools)
library(leaflet)
library(tidyverse)
options(scipen = 999)
nc_data <- st_read(system.file("shape/nc.shp", package="sf")) %>%
mutate(Code = case_when(
AREA <= 0.07 ~ "Label 1", TRUE ~ "Label 2"
))
tm_shape(nc_data)+
tm_polygons("Code", id = "NAME", palette = "Blues")
tmap_mode("view")
tmap_last()
同样,我如何为其他州和多个州绘制此图?
'nc' 数据作为示例数据包含在 sf
包中,因此您必须以其他方式获取其他州的数据。
有几种方法可以绘制县级和州级 shapefile。 urbnmapr(https://github.com/UrbanInstitute/urbnmapr)就是我下面用的那个
library(tidyverse)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(urbnmapr)
library(tmap)
us_counties <- get_urbn_map(map = 'counties', sf = TRUE)
us_counties <- us_counties %>%
mutate(area_m_sq = st_area(.))
tmap_mode('plot')
#> tmap mode set to plotting
tm_shape(us_counties) +
tm_polygons('area_m_sq', palette = 'Blues')
由 reprex package (v1.0.0)
于 2021-02-19 创建urbnmapr 将 HI 和 AK 移动到美国西部以南的通常插入点。如果这是一个问题,你可能会更好地从底格里斯包 https://github.com/walkerke/tigris.
获取数据