2

我正在尝试 R 制图包。不得不努力找到一个想要处理制图的美国州 shapefile - 许多似乎太大了,等等。我似乎让一切顺利,但科罗拉多州的错误情节。

library(cartography)
library(sf)
library(RColorBrewer)
library(maps)
library(ggplot2)

rm(list = ls())


# USA shape file
states <- st_as_sf(map("state", plot = F, fill = TRUE))

#seems to plot correctly here
#ggplot(states) + geom_sf(aes(fill = ID))

usa <- st_transform(states, 
                    CRS("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96"))

# still seems to plot fine
#ggplot(usa) + geom_sf(aes(fill = ID))

usa <- st_buffer(usa, dist=0)

datamap <- usa

datamap$randoVar <- sample(1:3, length(datamap$ID), replace = T)

datamap_pencil <- getPencilLayer(
  x = datamap, 
  buffer = 500,
  size = 400, 
  lefthanded = F
)

plot(st_geometry(usa), col = "white", border = "black", bg = "lightblue1")

typoLayer(
  x = datamap_pencil,
  var="randoVar",
  col = c("aquamarine4", "yellow3","#3c5cb0"),
  lwd = .7,
  legend.values.order = 1:3,
  legend.pos = "bottomleft",
  legend.title.txt = "",
  add = TRUE
)

labelLayer(x = datamap, txt = "ID", 
           cex = 0.9, halo = TRUE, r = 0.15)

我首先注意到是因为当我尝试合并数据文件并使用该功能进行填充时,科罗拉多州出现“无数据”。同样,上面的代码似乎表明状态几何或 ID 已关闭。我不知道足够的 GIS 来理解为什么。我确实必须更改 CRS 投影,以便可以缓冲地图文件(getPencilLayer 不断抛出自我拦截错误,这似乎在 R 映射中很常见)。

在此处输入图像描述

关于做什么的任何想法?

4

2 回答 2

1

好吧,我最终通过使用来自美国人口普查的 shapefile 进行了修复

https://www2.census.gov/geo/tiger/TIGER2017/STATE/

states <- st_read("#mypath#/tl_2017_us_state/tl_2017_us_state.shp")
states <- states[!(states$NAME %in% 
                     c("Commonwealth of the Northern Mariana Islands", "United States Virgin Islands", 
                       "Puerto Rico", "American Samoa", "Hawaii", "Guam", "Alaska")
), ]

usa <- st_transform(states, 
                     CRS("+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96"))

usa <- st_buffer(usa, dist=0)

不知道如何调整以从 map("state"...) 获取地理日期,但这对我有用。

使用了这些缓冲区和大小设置(在合并数据后)

datamap_pencil <- getPencilLayer(
  x = datamap, 
  buffer = 500,
  size = 400, 
  lefthanded = F
)
于 2020-02-07T22:54:57.897 回答
0

已经在这里回答: https ://gis.stackexchange.com/a/351910/142200

问题是初始地图对象无效

library(cartography)
library(sf)
library(RColorBrewer)
library(maps)
library(ggplot2)

rm(list = ls())
# USA shape file
states <- st_as_sf(map("state", plot = F, fill = TRUE))

usa <- st_transform(states,
                    "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96")


datamap <- usa
# Check validity----
st_is_valid(datamap)
#>  [1]  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [13]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [25]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [37]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
#> [49]  TRUE

#Make valid
library(lwgeom)
datamap<-st_make_valid(datamap)
st_is_valid(datamap)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> [46] TRUE TRUE TRUE TRUE

# Start cartography

datamap$randoVar <- sample(1:3, length(datamap$ID), replace = T)

datamap_pencil <- getPencilLayer(
  x = datamap, 
  buffer = 500,
  size = 400, 
  lefthanded = F
)
plot(st_geometry(usa), col = "white", border = "black", bg = "lightblue1")
typoLayer(
  x = datamap_pencil,
  var="randoVar",
  col = c("aquamarine4", "yellow3","#3c5cb0"),
  lwd = .7,
  legend.values.order = 1:3,
  legend.pos = "bottomleft",
  legend.title.txt = "",
  add = TRUE
)

在此处输入图像描述

reprex 包于 2020-02-25 创建(v0.3.0)

于 2020-02-25T17:05:22.180 回答