在 R 中将值放在县地图上

Putting Values on a County Map in R

我正在使用 excel sheet 作为数据。一列包含 GA 县的 FIPS 编号,另一列标有数字 1 - 5 的计数。我使用以下代码制作了包含这些值的地图:

library(usmap)  
library(ggplot2)  
library(rio)  
carrierdata <- import("GA Info.xlsx")  
plot_usmap( data = carrierdata, values = "Count", "counties", include = c("GA"), color="black") +  
    labs(title="Georgia")+  
    scale_fill_continuous(low = "#56B1F7", high = "#132B43", name="Count", label=scales::comma)+  
    theme(plot.background=element_rect(), legend.position="right")  

我已经包括了我得到的地图图片和我正在使用的数据样本。谁能帮我输入每个县的实际计数?
谢谢!
Data

usmap 包是县地图的一个很好的来源,但它包含的数据是县 outlines[=38 的 x,y 坐标的数据框格式=],而您需要在县的 中心 中绘制的数字。该包裹似乎不包含每个县的中心坐标。

虽然有点麻烦,但值得将地图转换为正式的 sf 数据框格式,以提供更好的绘图选项,包括计算每个县的质心。首先,我们将加载必要的包,获取格鲁吉亚数据并将其转换为 sf 格式:

library(usmap)
library(sf)
library(ggplot2)
 
d   <- us_map("counties")
d   <- d[d$abbr == "GA",]
GAc <- lapply(split(d, d$county), function(x) st_polygon(list(cbind(x$x, x$y))))
GA  <- st_sfc(GAc, crs = usmap_crs()@projargs)
GA  <- st_sf(data.frame(fips = unique(d$fips), county = names(GAc), geometry = GA))

现在,显然我没有你的数字数据,所以我必须弥补一些,相当于你从 Excel 导入的数据。我假设您自己的 carrierdata 有一个名为“fips”的列和另一个名为“values”的列:

set.seed(69)
carrierdata <- data.frame(fips = GA$fips, values = sample(5, nrow(GA), TRUE))

所以现在我们 left_join 将数据导入到 GA 县数据:

GA <- dplyr::left_join(GA, carrierdata, by = "fips")

我们可以计算每个县的中心点:

GA$centroids <- st_centroid(GA$geometry)

现在剩下的就是绘制结果:

ggplot(GA) + 
  geom_sf(aes(fill = values)) + 
  geom_sf_text(aes(label = values, geometry = centroids), colour = "white")