如何在R中用FIPS代码绘制北卡县级数据交互式热力图(支持年份/变量选择)
搞定北卡罗莱纳州各县地图:静态+交互式方案(支持年份&变量选择)
嘿,我帮你解决这个地图需求!不管你想要只显示北卡的静态地图,还是带下拉菜单选年份、变量的交互式版本,这就给你一步步安排明白~
一、先把数据和地理文件准备好
首先咱们先把你的示例数据理清楚,再拿到北卡各县的地理数据,这样才能精准匹配:
# 先加载需要的包,缺一不可哦 library(tidyverse) library(tmap) library(sf) library(maps) library(maptools) library(shiny) # 做交互式下拉菜单用 # 你的示例数据,我加了个随机种子保证结果能复现 set.seed(123) example <- data.frame( fips = rep(as.numeric(c("37001", "37003", "37005", "37007", "37009", "37011", "37013", "37015", "37017", "37019")), 4), year = c(rep(1990, 10), rep(1991, 10), rep(1992, 10), rep(1993, 10)), life = sample(1:100, 40, replace=TRUE), income = sample(8000:1000000, 40, replace=TRUE), pop = sample(80000:1000000, 40, replace=TRUE) ) # 拿北卡各县的地理数据(用sf格式更方便后续操作) nc_counties <- sf::st_as_sf(map("county", "north carolina", plot = FALSE, fill = TRUE)) # 给地理数据加上fips编码,和你的数据匹配上 nc_fips <- county.fips %>% mutate(county = tolower(paste(region, subregion, sep = ","))) %>% filter(str_detect(county, "^north carolina,")) %>% select(fips, county) # 把地理数据和你的指标数据合并到一起 nc_data <- nc_counties %>% mutate(county = tolower(paste(region, subregion, sep = ","))) %>% left_join(nc_fips, by = "county") %>% mutate(fips = as.numeric(fips)) %>% left_join(example, by = "fips")
二、静态地图方案:只显示北卡,按需选年份变量
1. 一次性生成所有年份+变量的分面地图
如果想一次性看全所有年份和变量的情况,用分面地图最直观:
# 先把数据转成长格式,方便处理多变量 nc_data_long <- nc_data %>% pivot_longer(cols = c(life, income, pop), names_to = "variable", values_to = "value") # 切换到tmap的静态绘图模式 tmap_mode("plot") tm_shape(nc_data_long) + tm_polygons("value", palette = "viridis", # 这个配色好看还友好 title = "指标数值") + tm_facets(by = c("year", "variable"), ncol = 2) + # 按年份和变量分面 tm_layout(main.title = "北卡各县年度指标对比", legend.outside = TRUE)
2. 手动指定年份和变量的单张地图
如果只想看某一年的某个变量,比如1990年的预期寿命,这么写:
# 自己选年份和变量 selected_year <- 1990 selected_var <- "life" tmap_mode("plot") tm_shape(filter(nc_data, year == selected_year)) + tm_polygons(selected_var, palette = "RdPu", title = str_to_title(selected_var)) + # 把变量名首字母大写,看起来更舒服 tm_layout(main.title = paste("1990年北卡各县", str_to_title(selected_var)), legend.position = c("right", "bottom"))
3. 修复你之前的全美国地图问题
你之前的代码画了整个美国,导致北卡太小,只需要在map()函数里指定画北卡就行:
library(maps) library(mapproj) data(county.fips) colors = c("#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043") # 先筛选1990年的life数据来演示 example_1990 <- filter(example, year == 1990) example_1990$colorBuckets <- as.numeric(cut(example_1990$life, c(0, 20, 40, 60, 80, 90, 100))) # 只拿北卡的县fips来匹配 nc_county_fips <- county.fips %>% mutate(county = tolower(paste(region, subregion, sep = ","))) %>% filter(str_detect(county, "^north carolina,")) colorsmatched <- example_1990$colorBuckets[match(nc_county_fips$fips, example_1990$fips)] # 重点!这里指定画north carolina的county,就不会画全美国了 map("county", "north carolina", col = colors[colorsmatched], fill = TRUE, resolution = 0, lty = 0, projection = "polyconic") title("1990年北卡各县预期寿命")
三、交互式地图:下拉菜单选年份&变量
1. 简单交互式地图(tmap自带交互)
如果只需要基础的点击查看详情、切换图层,直接用tmap的视图模式就行:
tmap_mode("view") tm_shape(nc_data_long) + tm_polygons("value", palette = "viridis", popup.vars = c("年份" = "year", "指标" = "variable", "数值" = "value")) + # 点击显示信息 tm_facets(by = c("year", "variable"), sync = TRUE) # 同步缩放平移
2. 带下拉菜单的交互式地图(Shiny实现)
如果想要明确的下拉菜单来选择年份和变量,写个简单的Shiny App就能搞定,运行后就能实时切换:
ui <- fluidPage( titlePanel("北卡各县交互式地图"), sidebarLayout( sidebarPanel( # 年份下拉菜单 selectInput("year", "选择年份:", choices = unique(nc_data$year)), # 变量下拉菜单(给变量起个中文名字更直观) selectInput("variable", "选择指标:", choices = c("预期寿命" = "life", "收入" = "income", "人口" = "pop")) ), mainPanel( tmapOutput("map") ) ) ) server <- function(input, output) { output$map <- renderTmap({ tmap_mode("view") tm_shape(filter(nc_data, year == input$year)) + tm_polygons(input$variable, # 匹配变量名 palette = "RdPu", popup.vars = c("县名" = "subregion", "年份" = "year", input$variable = input$variable)) + # 弹窗显示信息 tm_layout(title = paste(input$year, "年北卡各县", names(input$variable))) }) } # 运行App shinyApp(ui, server)
运行这个代码后,你就能通过左侧的下拉菜单随意切换年份和变量,地图只显示北卡各县,完全符合你的需求~
内容的提问来源于stack exchange,提问作者cskn
相关产品推荐
相关产品推荐

