如何在r中的空间覆盖中包括夏威夷和阿拉斯加

How to include Hawaii and Alaska in spatial overlay in R

我在获取人口统计数据点以覆盖美国县级地图时遇到了一些麻烦。我可以很好地绘制地图,但没有显示夏威夷和阿拉斯加的数据。我已经确定了问题的根源 - 在我的 over 命令之后。我的工作流程使用了一个 csv 文件,可以在此处找到 (https://www.dropbox.com/s/0arazi2n0adivzc/data.dem2.csv?dl=0)。这是我的工作流程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#Load dependencies
devtools::install_github("hrbrmstr/albersusa")
library(albersusa)
library(dplyr)
library(rgeos)
library(maptools)
library(ggplot2)
library(ggalt)
library(ggthemes)
library(viridis)

#Read Data
df<-read.csv("data.dem.csv")

#Retreive polygon shapefile
counties_composite() %>%
  subset(df$state %in% unique(df$state)) -> usa #Note I've checked here and Alaska is present, see below

enter

1
2
3
4
5
#Subset just points and create spatial points object
pts <- df[,4:1]
pts<-as.data.frame(pts)
coordinates(pts) <- ~long+lat
proj4string(pts) <- CRS(proj4string(usa))  #Note I've checked here as welland Alaska is present still, see here

enter

1
2
#Spatial overlay
b<-over(pts, usa) #This is where the problem arises: see here

enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
b<-select(b, -state)
b<-bind_cols(df, b)

bind_cols(df, select(over(pts, usa), -state)) %>%
  count(fips, wt=count) -> df

usa_map <- fortify(usa, region="tips")

ggplot()+
geom_map(data=usa_map, map=usa_map,
                    aes(long, lat, map_id=id),
                    color="#b2b2b2", size=0.05, fill="grey") +
geom_map(data=df, map=usa_map,
                    aes(fill=n, map_id=fips),
                    color="#b2b2b2", size=0.05) +
scale_fill_viridis(name="Count", trans="log10") +
gg + coord_map() +
theme_map() +
theme(legend.position=c(0.85, 0.2))

您可能会怀疑,最终输出没有显示阿拉斯加或夏威夷的数据。我不确定发生了什么,但似乎 sp 包中的 over 命令是问题的根源。非常感谢任何建议。

enter

你需要做的比之前的答案多一点,因为复合 shapefile - 根据它的定义 - 移动阿拉斯加