地图上各县的颜色

Color in counties on a map

我有一个县列表,我想在地图上涂上颜色,但我发现唯一的方法是使用 lat/long。这并不准确,因为我想确保所有县都有代表,而不仅仅是一个点。

县界可以涂色吗?

示例:

输出:

    db_loc <- structure(list(X = 1:10, fips = c(5001L, 5001L, 5001L, 5001L, 
5001L, 5001L, 5001L, 5001L, 5001L, 5001L), zip = c(72003L, 72026L, 
72038L, 72042L, 72048L, 72055L, 72073L, 72140L, 72160L, 72166L
), city = c("Almyra", "Casscoe", "Crocketts Bluff", "De Witt", 
"Ethel", "Gillett", "Humphrey", "Saint Charles", "Stuttgart", 
"Tichnor"), latitude = c(34.403216, 34.505369, 34.438327, 34.283347, 
34.28965, 34.109348, 34.396301, 34.383661, 34.479852, 34.061917
), longitude = c(-91.40953, -91.30213, -91.26907, -91.32515, 
-91.13632, -91.36875, -91.66201, -91.15428, -91.53854, -91.24828
)), .Names = c("X", "fips", "zip", "city", "latitude", "longitude"
), row.names = c(NA, 10L), class = "data.frame")

地图数据:

library(ggmap)

map <- get_map(location='arkansas', zoom=8, maptype = "terrain",
             source='google',color='color')

ggmap(map) + geom_point(
                aes(x=longitude, y=latitude, show_guide = TRUE), 
                data=db_loc, alpha=.8, na.rm = T) 

输出:

R 中有一些包可以帮助您开箱即用。这里有充足的说明和示例:
https://github.com/arilamstein/choroplethr

你应该可以通过标准安装来安装 chroplether,我认为:
install.packages("choroplethr")

如果您愿意,可以从此处的美国人口普查局网站获取您自己的地图:

截至 2015 年 10 月 9 日,此 link 有效:
https://www.census.gov/geo/maps-data/data/cbf/cbf_counties.html

获取 shapefile 或 kml 文件。如果您不知道这些是什么,只需 google 即可。 rgdal 包应该读到。再次,阅读手册。这将为您提供一个包含所有县轮廓的结构。


大多数县有 FIPS code, you must key your data on either the FIPS code or on the geoIDs from the rgdal file. Choroplethr should automate this to some degree. Just follow their examples here


如果这仍然太难,这里有一个完整的例子。我没有使用你的数据,因为它只包含一个县(FIPS 05001,阿肯色县)。

library(ggplot2)
set.seed(1)    # for reproducible example
map.county <- map_data('county')
counties   <- unique(map.county[,5:6])
akcounties <- counties[counties$region=="arkansas",]
akcounties

#This is where you'd select whatever you want to plot
# `Obesity` is the first thing to come to mind
# no offense meant to the people of the great state of Arkansas :)
# it's defined as a random uniform draw between 0-100 in the 3rd line below:
obesity_map <- data.frame(state_names=akcounties$region, 
                          county_names=akcounties$subregion, 
                          obesity= runif(nrow(akcounties), min=0, max=100))

# 用你想要绘制的任何变量替换上面的值, # 以及您真正关心的部分县

library(data.table)   # use data table merge - it's *much* faster
map.county <- data.table(map.county[map.county$region=="arkansas",])
setkey(map.county,region,subregion)
obesity_map <- data.table(obesity_map)
setkey(obesity_map,state_names,county_names)
map.df      <- map.county[obesity_map]

ggplot(map.df, aes(x=long, y=lat, group=group, fill=obesity)) + 
geom_polygon()+coord_map()