Skip to content

Instantly share code, notes, and snippets.

@igproj-fusion
Created April 22, 2024 03:12
Show Gist options
  • Save igproj-fusion/3fee51080491ac757fca8103d7c31e03 to your computer and use it in GitHub Desktop.
Save igproj-fusion/3fee51080491ac757fca8103d7c31e03 to your computer and use it in GitHub Desktop.
#############################################################################
#
# 人口動態統計特殊報告
#
# 平成30年~令和4年 人口動態保健所・市区町村別統計
# https://www.mhlw.go.jp/toukei/saikin/hw/jinkou/other/hoken24/index.html
#
#############################################################################
pacman::p_load(
rvest,
xlsx,
tidyverse,
sf)
"%!in%" <- Negate("%in%")
NorthernTerr <- c("01695", "01696", "01697",
"01698", "01699", "01700")
URL0 <- "https://www.e-stat.go.jp"
URL1 <- "/stat-search/files?page=1&layout=datalist"
URL2 <- "&toukei=00450013&tstat=000001217081&cycle=7"
URL3 <- "&result_page=1&tclass1val=0"
link <- read_html(paste0(URL0, URL1, URL2, URL3)) |>
html_nodes("a") |>
html_attr("href")
DLfiles <-paste0(URL0, link[grepl(pattern = "?statInfId=", x = link)])
tmp_dir <- tempdir()
dest_file <- file.path(tmp_dir, "9.xlsx")
download.file(DLfiles[9], destfile = dest_file, mode = "wb")
TFR <- read.xlsx(dest_file, sheetIndex = 1,
startRow = 6, header = FALSE) |>
select(code = X1, tfr = X5)
JP_geo <- "https://github.com/igproj-fusion/R-gis/raw/main/ESRI_japan_84.geojson"
JP <- read_sf(JP_geo) |>
filter(JCODE %!in% NorthernTerr) |>
filter(SIKUCHOSON != "所属不明地") |>
mutate(JCODE = as.integer(JCODE)) |>
left_join(TFR, by = c("JCODE" = "code"))
###############################
# 千葉県 plot
###############################
JP |>
filter(KEN == "千葉県") |>
ggplot() +
geom_sf(aes(fill = tfr)) +
scale_fill_gradient(high = "#f8f8a3", low = "#ff7f00") +
labs(fill = "TFR") +
theme_void()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment