US map

Author

田中 鮎夢

usmapパッケージ

  • インストール
install.packages("usmap")
vignette("usmap1", package = "usmap") # 1. Introduction
vignette("usmap2", package = "usmap") # 2. Mapping the US
vignette("usmap3", package = "usmap") # 3. Advanced Mapping
  • パッケージの読み込み
# パッケージの読み込み
library(usmap) 
library(ggplot2) 

sfパッケージ

  • 本来必要ないはずだが、入れておかないとエラーが出る。
library(sf)
#sf::st_crs(states) <- 9311
#sf::st_crs(counties) <- 9311

白地図1

# 白地図
states <- usmap::plot_usmap("states")
counties <- usmap::plot_usmap("counties")
cowplot::plot_grid(states, counties, nrow = 1)

白地図2

plot_usmap("states", labels = TRUE)

主題図

state <- map_data("state")

ggplot(data=state, aes(x=long, y=lat, fill=region, group=group)) + 
  geom_polygon(color = "white") + 
  guides(fill=FALSE) + 
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(),
        axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) + 
  ggtitle('U.S. Map with States') + 
  coord_fixed(1.3)

usdata

  • usdataパッケージを使うと、州の名前とコードを変換できる。
  • 州のコードをkeyにして、外部データと接続することができる。
  • 接続したあとに、state$orderでソートが必要である。
# データ
state <- map_data("state")
head(state)
       long      lat group order  region subregion
1 -87.46201 30.38968     1     1 alabama      <NA>
2 -87.48493 30.37249     1     2 alabama      <NA>
3 -87.52503 30.37249     1     3 alabama      <NA>
4 -87.53076 30.33239     1     4 alabama      <NA>
5 -87.57087 30.32665     1     5 alabama      <NA>
6 -87.58806 30.32665     1     6 alabama      <NA>
# 州の名前からコードを取得
library(usdata)
state$state_code <- state2abbr(state$region)
head(state)
       long      lat group order  region subregion state_code
1 -87.46201 30.38968     1     1 alabama      <NA>         AL
2 -87.48493 30.37249     1     2 alabama      <NA>         AL
3 -87.52503 30.37249     1     3 alabama      <NA>         AL
4 -87.53076 30.33239     1     4 alabama      <NA>         AL
5 -87.57087 30.32665     1     5 alabama      <NA>         AL
6 -87.58806 30.32665     1     6 alabama      <NA>         AL