【R】長崎県の人口別コロプレス図
2020年9月10日
長崎県の市区町村を人口別に塗りつぶしてみます。境界の描画は「地図で見る統計(統計GIS)」の「42000 長崎県全域 世界測地系緯度経度・Shapefile」から取得します。
library(ggplot2)
library(ggrepel)
library(sf)
library(tidyverse)
library(kableExtra)
map <- read_sf("shp/h27ka42.shp") # 長崎県のシェープファイル
dat <- map %>%
group_by(CITY_NAME) %>% # 市名でグルーピング
summarize('Pop.total' = sum(JINKO)) %>% # グループ単位で領域結合, 人口計算
mutate( # 重心計算と座標値の抽出
centroid = st_centroid(geometry),
x = st_coordinates(centroid)[, 1],
y = st_coordinates(centroid)[, 2]
) %>%
arrange(desc(Pop.total))
dat %>%
ggplot() +
geom_sf(aes(fill = Pop.total)) + # 人口毎に色分け
coord_sf(datum = NA) +
scale_fill_viridis_c(alpha = 0.6) +
theme_void()+
geom_text_repel(aes(x = x, y = y, label = CITY_NAME), col="black",
family = "JP4", size = 3)
table_df<-data.frame(市区町村=dat$CITY_NAME, 人口=dat$Pop.total)
library(clipr)
table_df %>%
kable(align = "c", row.names=FALSE) %>%
kable_styling(full_width = F) %>%
column_spec(1, bold = T) %>%
collapse_rows(columns = 1, valign = "middle") %>%
write_clip

| 市区町村 | 人口 |
|---|---|
| 長崎市 | 429508 |
| 佐世保市 | 255439 |
| 諫早市 | 138078 |
| 大村市 | 92757 |
| 南島原市 | 46535 |
| 島原市 | 45436 |
| 雲仙市 | 44115 |
| 長与町 | 42548 |
| 五島市 | 37327 |
| 平戸市 | 31920 |
| 対馬市 | 31457 |
| 時津町 | 29804 |
| 西海市 | 28691 |
| 壱岐市 | 27103 |
| 松浦市 | 23309 |
| 新上五島町 | 19718 |
| 波佐見町 | 14891 |
| 川棚町 | 14067 |
| 佐々町 | 13626 |
| 東彼杵町 | 8298 |
| 小値賀町 | 2560 |