如何在R中绘制美国多个州的轮廓?

MMM*_*MMM 5 r ggplot2

我想使用经度和纬度点在 R 中绘制美国多个州的轮廓。现在我只能画出每个州的轮廓,所以我想知道如何画出美国多个州的轮廓(仅限外线)。

library(tidyverse)
library(knitr)    
library(broom)
library(stringr)
library(modelr)
library(forcats)
library(ggmap)


states <- map_data("state")# %>% as_tibble()
counties <- map_data("county")
il_df <- filter(fifty_states, id == "illinois")
midwest <- subset(fifty_states, id %in% c("illinois", "indiana", "iowa",
                                    "kansas", "michigan", "minnesota",
                                    "missouri", "nebraska", "north dakota",
                                    "ohio", "south dakota", "wisconsin"))


il_mid <- ggplot(data = midwest, mapping = aes(x = long, y = lat, group = group)) + 
  coord_fixed(1.3) + 
  geom_polygon(color = "black", fill = "gray")
il_mid
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想知道如何绘制下图中的粉红色线。

在此输入图像描述

Cal*_*You 6

这是一种使用sf包的方法,并且geom_sfggplot2. 我从提供阿拉斯加和夏威夷作为插图的好包中获取地图数据fiftystater,但需要转换为sf格式。我使用这里的答案来做到这一点。

那么剩下的就很简单了,我们只需filter找到正确的状态,将它们与 结合在一起summarise.sf,然后用geom_sf来绘制结果即可。

library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3
library(fiftystater)

sf_fifty <- sf::st_as_sf(fifty_states, coords = c("long", "lat")) %>% 
  group_by(id, piece) %>% 
  summarize(do_union = FALSE) %>%
  st_cast("POLYGON") %>% 
  ungroup()

midwest <- sf_fifty %>%
  filter(
    id %in% c(
      "illinois", "indiana", "iowa",
      "kansas", "michigan", "minnesota",
      "missouri", "nebraska", "north dakota",
      "ohio", "south dakota", "wisconsin"
    )
  ) %>%
  summarise(id = "midwest")

ggplot() +
  theme_minimal() +
  geom_sf(data = sf_fifty) +
  geom_sf(data = midwest, col = "hotpink", alpha = 0, size = 2)
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.2.0)于 2018 年 5 月 24 日创建。