【发布时间】:2016-07-17 05:38:14
【问题描述】:
我在获取人口统计数据点以覆盖美国县级地图时遇到了一些麻烦。我可以很好地绘制地图,但没有显示夏威夷和阿拉斯加的数据。我在over 命令之后确定了问题的根源。我的工作流程使用了一个 csv 文件,可以在此处找到 (https://www.dropbox.com/s/0arazi2n0adivzc/data.dem2.csv?dl=0)。这是我的工作流程:
#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
#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
#Spatial overlay
b<-over(pts, usa) #This is where the problem arises: see here
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 命令是问题的根源。任何建议都非常感谢。
请注意,这是一个不同于Relocating Alaska and Hawaii on thematic map of the USA with ggplot2 和How do you create a 50 state map (instead of just lower-48) 的问题
这些问题彼此无关。这不是重复的。第一个问题是关于夏威夷和阿拉斯加实际多边形的位置,正如您从我的地图中看到的那样,我没有这个问题。第二个链接是关于获取包含夏威夷和阿拉斯加的地图。同样,我的地图包括这两个,但在我的数据处理工作流程的某个地方,这两个的数据被删除(特别是覆盖功能)。请不要标记为重复。
【问题讨论】:
标签: r overlay spatial choropleth