无法在 r 中构建等值线图

Unable to construct choropleth map in r

我有一些人口统计数据,我想用它来制作美国各县的等值线图。我的工作流程没有 运行 出现任何错误,我能够创建最终地图,但是,其映射的数据不正确。我的工作流程使用两个数据源——一个形状文件和一个 data.frame。 shapefile 是一个县级 shapefile,可以在此 link https://www.dropbox.com/s/4ujxidyx42793j7/cb_2015_us_county_500k.zip?dl=1 找到 data.frame 文件可以在 link 找到:https://www.dropbox.com/s/qys6s6ikrs1g2xb/data.dem.csv?dl=1

这是我的代码:

#Load dependencies
library(sp)
library(spatialEco)
library(rgdal)
library(dplyr)
library(maptools)
library(taRifx.geo)
library(ggplot2)
library(USAboundaries)
library(splitstackshape)
library(maps)
library(cowplot)

#Read in shape and csv files
county.track<-readOGR("/path", "filename")
county.track@data$id = rownames(county.track@data)
data<-read.csv("/path/filename.csv")

#Convert data.frame (data) to points polygon file
data$y<-data$lat
data$x<-data$long
coordinates(data) <- ~ x + y
proj4string(data) <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
proj4string(county.track) <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")

#Overlay points onto polygons
county.track.data<-point.in.poly(data, county.track)

#Summarize point data by county
count<-select(as.data.frame(county.track.data), id, count)
count<-count %>%
  group_by(id) %>%
  summarize(count=sum(count))

#Merge with shape file data
county.track@data<-merge(county.track@data, count, by="id", all.x=T)

#Replace NA values with zeroes 
county.track@data$count[is.na(county.track@data$count)]<-0
county.track.points = fortify(county.track, region="id")
map.plot<-merge(county.track.points, county.track@data, by="id")

#Get rid of Hawaii and Alaska
map.plot<-map.plot %>%
  filter(lat<50 & lat>25) %>%
  filter(long>-130)

#Create choropleth map using ggplot2
 ggplot(map.plot) +
  geom_polygon(aes(long, lat, group=group, fill=log(count))) +
  coord_map()

输出如下所示:

但这是错误的,原因有很多。第一,最明显的是很多数据没有映射。地图上的灰色区域表示 NA。但是我在上述步骤之一中删除了 NA,同样在检查用于映射的数据 (map.plot) 时,填充变量 (count) 中没有 NA。其次,映射内容的值分布是关闭的。洛杉矶县的计数值最高,为 793(对数值为 6.675823),但在地图上,许多颜色较浅的县表明其他空间单位的值更高,一些排名靠前的县(例如圣地亚哥)未填充完全没有(地图左下角)。

当我检查用于映射的数据 (map.plot) 时,一切似乎都正常。洛杉矶县仍然是 "count" 变量值最高的县,但地图显示情况并非如此(参见此处的这张图片)。 我希望有人可以在这里做一些取证并找出问题所在,我已尽力完成所有步骤,但我似乎无法找出问题所在。提前致谢。

更新:我尝试使用来自同一来源的不同形状文件。上面 link 中的 shapefile 与下面 (https://www.census.gov/geo/maps-data/data/cbf/cbf_counties.html) 中标记为 "cb_2015_us_county_500k.zip" 的相同。当我选择不同的 shapefile(例如 cb_2015_us_county_5m.zip)时,我得到了不同的地图但问题相同:请参阅以下地图示例:

我不确定发生了什么!在这张新地图中,洛杉矶县甚至不再有颜色,但奥兰治县有!非常感谢任何帮助。

不太确定你的合并发生了什么,但这对我有用:

library(albersusa) # devtools::install_github("hrbrmstr/albersusa)
library(readr)
library(dplyr)
library(rgeos)
library(maptools)
library(ggplot2)
library(ggalt)
library(ggthemes)
library(viridis)

df <- read_csv("data.dem.csv")

counties_composite() %>% 
  subset(state %in% unique(df$state)) -> usa

pts <- df[,2:1]
coordinates(pts) <- ~long+lat
proj4string(pts) <- CRS(proj4string(usa))

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

您共有 942 个县:

glimpse(df)
## Observations: 942
## Variables: 2
## $ fips <chr> "01001", "01003", "01013", "01015", "01043", "01055", "01061", ...
## $ n    <int> 1, 2, 1, 3, 1, 3, 1, 1, 19, 6, 12, 7, 7, 1, 4, 4, 1, 5, 67, 19,...

U.S 中有超过 3000 个县。

但是NA数量不多:

filter(df, is.na(fips))
## # A tibble: 1 x 2
##    fips     n
#3   <chr> <int>
## 1  <NA>    10

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

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