Skip to content

Instantly share code, notes, and snippets.

@uribo
Created September 10, 2019 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uribo/90ba00dbc7cb98ade42907adca4b80ac to your computer and use it in GitHub Desktop.
Save uribo/90ba00dbc7cb98ade42907adca4b80ac to your computer and use it in GitHub Desktop.
#############################################
# 東京電力街が提供する千葉県内の停電状況
#############################################
library(dplyr)
library(sf)
library(ggplot2)
library(rcartocolor)
library(ggrepel)
library(gridExtra)
if (file.exists(here::here("data-raw/tepco_blackout_20190910_2300_pref12.rds")) == FALSE) {
library(rvest)
site_url <- "http://teideninfo.tepco.co.jp/html"
x <-
glue::glue(site_url, "/12000000000.html") %>%
read_html()
x %>%
html_nodes(css = "#main_container > p:nth-child(3)") %>%
html_text(trim = TRUE)
df_pref12_blackout <-
x %>%
html_nodes(css = "#main_container > table.bo_lv2 > tbody > tr") %>%
purrr::map_dfr(
~ tibble::tibble(
city_name = .x %>% html_nodes(css = "td.bo_lv2_occur > a") %>% html_text(),
value = .x %>% html_nodes(css = "td.bo_lv2_occur_r") %>% html_text(),
url = .x %>% html_nodes(css = "td.bo_lv2_occur > a")%>% html_attr("href"))) %>%
mutate(url = paste(site_url, url, sep = "/"))
collect_city_blackout_table <- function(url) {
x <-
url %>%
read_html()
tibble::tibble(
city_code = stringr::str_remove(basename(url), ".html"),
city_name = x %>%
html_nodes(css = "#area_left") %>%
html_text(trim = TRUE) %>%
stringr::str_split("-", simplify = TRUE) %>%
purrr::pluck(3) %>%
stringr::str_trim(),
name = x %>%
html_nodes(css = "#main_container > table.bo_lv3 > tbody > tr > td:nth-child(odd)") %>%
html_text(trim = TRUE),
value = x %>%
html_nodes(css = "#main_container > table.bo_lv3 > tbody > tr > td:nth-child(even)") %>%
html_text(trim = TRUE)
) %>%
filter(name != "")
}
slw_collect_city_blackout_table <-
purrr::slowly(~ collect_city_blackout_table(.x),
rate = purrr::rate_delay(pause = 3),
quiet = FALSE)
df_pref12_blackout_town <-
df_pref12_blackout %>%
pull(url) %>%
purrr::map_dfr(slw_collect_city_blackout_table)
df_pref12_blackout_town <-
df_pref12_blackout_town %>%
mutate(under = stringr::str_detect(value, "未満")) %>%
mutate(value = readr::parse_number(value)) %>%
group_by(city_code, city_name, name) %>%
mutate(value = sum(value, na.rm = TRUE)) %>%
slice(1L) %>%
ungroup()
df_pref12_blackout_town %>%
readr::write_rds(here::here("data-raw/tepco_blackout_20190910_2300_pref12.rds"))
} else {
df_pref12_blackout_town <-
readr::read_rds(here::here("data-raw/tepco_blackout_20190910_2300_pref12.rds"))
}
# 地図 ----------------------------------------------------------------------
estat_district <- function(path) {
d <-
sf::st_read(path,
stringsAsFactors = FALSE,
as_tibble = TRUE) %>%
janitor::clean_names() %>%
dplyr::select(-area, -perimeter, -ken, -ken_name, -dummy1, -area_max_f,
-n_ken, -n_city, -kbsum, -x_code, -y_code, -kcode1,
-h27k_axx_id, -h27k_axx,
-kigo_e, -kigo_d, -kigo_i, -hcode,
-keycode1, -keycode2)
d <-
d %>%
dplyr::select(pref, pref_name, city, city_name, s_name, kihon1, jinko, setai) %>%
sf::st_drop_geometry() %>%
dplyr::group_by(city, kihon1) %>%
dplyr::mutate(jinko = sum(jinko),
setai = sum(setai)) %>%
dplyr::slice(1L) %>%
dplyr::ungroup() %>%
dplyr::left_join(d %>%
dplyr::group_by(city, kihon1) %>%
dplyr::summarise() %>%
dplyr::ungroup(),
by = c("city", "kihon1")) %>%
sf::st_sf() %>%
dplyr::mutate_if(is.character, stringi::stri_trans_nfkc)
d %>%
dplyr::mutate(s_name = if_else(stringr::str_detect(s_name, "丁目$"),
stringr::str_remove(s_name, "([一二三四五六七八九十壱弐参拾百千万萬億兆〇]|[0-9])丁目$"),
s_name),
s_name = stringr::str_remove_all(s_name, "[A-Z]$"))
}
sf_pref12 <-
estat_district("~/Documents/resources/e-Stat/2015_国勢調査_小地域/A002005212015DDSWC12/h27ka12.shp") %>%
filter(s_name != "水面", !is.na(s_name)) %>%
group_by(city, city_name, s_name) %>%
summarise(setai = sum(setai, na.rm = TRUE)) %>%
ungroup() %>%
select(city_name, s_name, setai) %>%
left_join(df_pref12_blackout_town %>%
mutate(city_name = recode(city_name,
`袖ケ浦市` = "袖ヶ浦市")),
by = c("city_name",
"s_name" = "name")) %>%
select(names(.)[!names(.) %in% attr(., "sf_column")])
p1 <-
sf_pref12 %>%
ggplot() +
geom_sf(aes(fill = value), color = "gray", size = 0.08) +
scale_fill_carto_c(palette = "OrYel", na.value = "white") +
theme_void(base_family = "IPAexGothic", base_size = 12) +
coord_sf(datum = NA) +
guides(fill = guide_colorbar(title = "停電件数\n(「約」および「未満」を含む)",
title.position = "top",
title.vjust = 0.95)) +
theme(legend.key.height = unit(3.0, "line"),
plot.caption = element_text(size = 6)) +
labs(title = "東京電力供給による千葉県内 地域別停電件数",
subtitle = "2019年9月10日 22:59現在",
caption = "データソース: http://teideninfo.tepco.co.jp/index-j.html\n千葉県行政区域: http://e-stat.go.jp/")
p2 <-
sf_pref12 %>%
mutate(city_name_s_name = paste(s_name, "\n(", city_name, ")")) %>%
top_n(10, value) %>%
ggplot(aes(forcats::fct_reorder(city_name_s_name, value), value, fill = value)) +
scale_fill_carto_c(palette = "OrYel", na.value = "white") +
geom_bar(stat = "identity") +
geom_text(aes(label = value), nudge_y = -600, color = "white") +
theme_bw(base_family = "IPAexGothic", base_size = 12) +
coord_flip() +
xlab(NULL) +
ylab("停電件数") +
guides(fill = FALSE) +
theme(legend.key.height = unit(3.0, "line")) +
labs(title = "東京電力供給による千葉県内 地域別停電件数 上位10地域",
subtitle = "2019年9月10日 22:59現在",
caption = "データソース: http://teideninfo.tepco.co.jp/index-j.html")
ggsave(filename = "tepco_blackout_20190910_2300_pref12_area.png",
plot = gridExtra::grid.arrange(p2, p1, nrow = 1),
width = 12, height = 5,
dpi = "retina")
sf_pref12_city <-
sf_pref12 %>%
group_by(city_name) %>%
summarise(value = sum(value, na.rm = TRUE)) %>%
mutate(value = if_else(value == 0, NA_real_, value)) %>%
mutate(value_c = case_when(
value < 1000 ~ "〜1000件",
between(value, 1001, 10000) ~ "1001〜10000件",
value > 10001 ~ "10001件〜"
))
p3 <-
sf_pref12_city %>%
ggplot() +
geom_sf(aes(fill = value_c), color = "gray", size = 0.08) +
geom_sf_text(data = . %>% filter(!is.na(value)),
aes(label = city_name), size = 1.2, family = "IPAexGothic") +
scale_fill_manual(values = c("〜1000件" = rgb(250, 245, 0, maxColorValue = 255),
"1001〜10000件" = rgb(255, 153, 0, maxColorValue = 255),
"10001件〜" = rgb(255, 40, 0, maxColorValue = 255))) +
theme_void(base_family = "IPAexGothic", base_size = 12) +
coord_sf(datum = NA) +
guides(fill = guide_legend(title = "停電件数\n(「約」および「未満」を含む)",
title.position = "top",
title.vjust = 0.95)) +
theme(legend.key.height = unit(2.0, "line")) +
labs(title = "東京電力供給による千葉県内 地域別停電件数",
subtitle = "2019年9月10日 22:59現在",
caption = "データソース: http://teideninfo.tepco.co.jp/index-j.html")
ggsave(filename = "tepco_blackout_20190910_2300_pref12_city.png",
plot = p3,
width = 7, height = 5,
dpi = "retina")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment