簡體   English   中英

無法在r中構建等值區域圖

[英]Unable to construct choropleth map in r

我有一些人口統計數據,我想用來制作美國各州的等值線圖。 我的工作流程沒有遇到任何錯誤,我可以創建最終的地圖,但是,它的映射不正確的數據。 我的工作流程使用兩個數據源 - 形狀文件和data.frame。 shapefile是一個縣形狀文件,可以在以下鏈接找到https://www.dropbox.com/s/4ujxidyx42793j7/cb_2015_us_county_500k.zip?dl=1可以在以下鏈接找到data.frame文件: 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)時,一切似乎都沒問題。 洛杉磯縣仍然是“計數”變量的最高價值縣,但地圖則另有建議(見此圖)。 在此輸入圖像描述 我希望有人可以在這里做一些取證並找出問題,我已經盡力完成所有步驟,但我似乎無法確定問題。 提前致謝。

更新:我嘗試使用來自同一來源的不同shapefile。 上面鏈接中的shapefile與下面標記為“cb_2015_us_county_500k.zip”的形狀文件相同( https://www.census.gov/geo/maps-data/data/cbf/cbf_counties.html )。 當我選擇不同的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,...

美國有超過3K個縣

但是, 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

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM