【问题标题】:Putting Values on a County Map in R在 R 中将值放在县地图上
【发布时间】:2020-07-20 21:35:22
【问题描述】:

我正在使用 Excel 表来获取数据。一列包含 GA 县的 FIPS 编号,另一列标记为 Count,编号为 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

【问题讨论】:

  • plot + geom_label(x=x, y=y, label = FIPS) 之类的东西 - 如果您可以添加数据快照会有所帮助:)

标签: r dictionary ggplot2 usmap


【解决方案1】:

usmap 包是县级地图的一个很好的来源,但它包含的数据是县级轮廓的 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")

【讨论】:

    猜你喜欢
    • 2020-07-08
    • 2012-12-06
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    相关资源
    最近更新 更多