You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言usmap包绘制的县级美国地图上添加州边界与缩写

你可以直接在现有绘图代码的基础上叠加两层元素实现需求,分别是州级边界层、州名缩写标注层,usmap包内置了对应的数据不需要额外导入,修改后的完整代码如下:

vec1 <- c(4013, 6037, 17031, 26163, 36059)
vec2 <- c(48045, 1009)
vec3 <- c(48289,48291)
library(ggplot2)
library(usmap)
library(dplyr)
library(stringr)

dt <- countypop %>%
  mutate(fill = case_when(
    fips %in% str_pad(vec1, 5, pad = "0") ~ "Blue",
    fips %in% str_pad(vec2, 5, pad = "0") ~ "Red",
    fips %in% str_pad(vec3, 5, pad = "0") ~ "Green",
    TRUE ~ "Other"
  ))

# 原绘图代码基础上新增两层
plot_usmap(regions = "counties", data = dt, values = "fill", color = "grey") +
  scale_fill_manual(
    values = c(Blue = "blue", Green = "green", Red = "red", Other = "light gray")
  ) +
  # 叠加州级边界,fill设为NA保证透明不遮挡下方县级填充色
  geom_polygon(
    data = us_map(regions = "states"),
    aes(x = x, y = y, group = group),
    fill = NA,
    color = "black",
    linewidth = 0.8
  ) +
  # 叠加州名缩写标注
  geom_text(
    data = centroid_labels(regions = "states"),
    aes(x = x, y = y, label = abbr),
    size = 2.8,
    color = "black"
  )

如果你觉得州名缩写太挤,可以调小geom_text里的size参数;如果需要调整边界粗细,修改geom_polygon里的linewidth参数即可。usmap内置的州级数据和县级数据用的是同一套投影,直接叠加不会出现错位问题。

内容的提问来源于stack exchange,提问作者Mathica

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 01:54:03