How to include Hawaii and Alaska in spatial overlay in R
我在获取人口统计数据点以覆盖美国县级地图时遇到了一些麻烦。我可以很好地绘制地图,但没有显示夏威夷和阿拉斯加的数据。我已经确定了问题的根源 - 在我的
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 |
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 |
1 2 | #Spatial overlay b<-over(pts, usa) #This is where the problem arises: see here |
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 包中的
请注意,这是一个与使用 ggplot2 在美国专题地图上重新定位阿拉斯加和夏威夷以及如何创建 50 个state的地图(而不仅仅是低于 48 个)的问题不同的问题
这些问题彼此无关。这不是重复的。第一个问题是关于夏威夷和阿拉斯加实际多边形的位置,正如您从我的地图中看到的那样,我没有这个问题。第二个链接是关于获取包含夏威夷和阿拉斯加的地图。同样,我的地图包含这两个,但在我的数据处理工作流程中的某个地方,这两个的数据被删除(特别是覆盖函数)。请不要标记为重复。
你需要做的比之前的答案多一点,因为复合 shapefile - 根据它的定义 - 移动阿拉斯加