# パッケージの読み込み
library(usmap)
library(ggplot2)
US map
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
- パッケージの読み込み
sf
パッケージ
- 本来必要ないはずだが、入れておかないとエラーが出る。
library(sf)
#sf::st_crs(states) <- 9311
#sf::st_crs(counties) <- 9311
白地図1
# 白地図
<- usmap::plot_usmap("states")
states <- usmap::plot_usmap("counties")
counties ::plot_grid(states, counties, nrow = 1) cowplot
白地図2
plot_usmap("states", labels = TRUE)
主題図
<- map_data("state")
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でソートが必要である。
# データ
<- map_data("state")
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_code <- state2abbr(state$region)
statehead(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