简体   繁体   English

将美国边界和缩写添加到带有县域的 USmap

[英]Add US boundaries and abbreviations to USmap with county regions

I have map below.from here .我下面有 map。从这里开始 how can I add state borders and abbreviated names to this county US map?如何将 state 边界和缩写名称添加到这个县 US map? 在此处输入图像描述

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")
  )

To plot the state borders you need to obtain the x, y coordinates for each state and draw them on the map. Since this is plotted with plot_usmap() , you will need to transform lat, long coordinates to the current plot coordinate system.对于 plot 和 state 边界,您需要获取每个 state 的 x、y 坐标并将它们绘制在 map 上。由于这是使用plot_usmap()绘制的,因此您需要将纬度、经度坐标转换为当前的 plot 坐标系。

states <- map_data("state") %>%
  usmap_transform()

Then add this to the plot with geom_polygon() .然后使用geom_polygon()将其添加到 plot。

  geom_polygon(data = states, aes(x = long.1,
                             y = lat.1,
                             group = group),
               color = "black",
               alpha = 0)

To add the state abbreviations you will need to first convert the lat, long data in the state.center data set to the coordinate system on the chart.要添加 state 缩写,您需要先将state.center数据集中的经纬度数据转换为图表上的坐标系。

state_centers <- data.frame(long = state.center$x,
                      lat = state.center$y) %>%
  usmap_transform()

Then merge this with data containing the names/abbreviations of each state. Since the data you have is missing Hawaii/Alaska, you need to remove those states.然后将其与包含每个 state 的名称/缩写的数据合并。由于您拥有的数据缺少夏威夷/阿拉斯加,因此您需要删除这些州。

centroids <- data.frame(name = state.name,
                          abb = state.abb) %>%
  mutate(center_long = state_centers$long.1,
         center_lat = state_centers$lat.1) %>%
  filter(abb != c("HI", "AK")) %>%
  mutate(name = tolower(name)) %>%
  rename("region" = name)

Then add the abbreviations to the plot with geom_text() .然后使用geom_text()将缩写添加到 plot。

  geom_text(data = state_names,
            aes(x = center_long,
                y = center_lat,
                label = abb))

Result:结果:

在此处输入图像描述

Things look pretty compact in the North East.东北部的情况看起来相当紧凑。 You may want to do something with that region of the country to make it more readable.您可能想对该国家/地区的那个地区做些事情以使其更具可读性。

If you don't want to worry about the coordinate transformations you can remake the plot as follows (not very pretty as-is, but could be styled easily):如果您不想担心坐标转换,您可以按如下方式重新制作 plot(不是很漂亮,但可以很容易地设置样式):

counties <- map_data("county")
states <- map_data("state")
state_centers <- data.frame(long = state.center$x,
                      lat = state.center$y)

centroids <- data.frame(name = state.name,
                          abb = state.abb) %>%
  mutate(center_long = state_centers$long,
         center_lat = state_centers$lat) %>%
  filter(abb != c("HI", "AK")) %>%
  mutate(name = tolower(name)) %>%
  rename("region" = name)

ggplot() +
  geom_polygon(data = counties, 
               aes(x = long,
                   y = lat, 
                   group = group),
               color = "grey",
               fill = "white") +
  geom_polygon(data = states,
               aes(x = long,
                   y = lat,
                   group = group),
               color = "black",
               alpha = 0) +
  geom_text(data = centroids,
            aes(x = center_long,
                y = center_lat,
                label = abb)) +
  coord_fixed(1.3)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM