library(ggthemes)
library(ggrepel)
library(httr)
library(jpndistrict)
library(sf)
library(tidyverse)
ne_states()
function cannot map down to
municipalities.jpndistrict
} package, published by Professor
Shinya Uryu of Tokushima University.jpndistrict
} package is necessary to draw the
map.jpndistrict
} package is not available for
download from CRAN.jpndistrict
} package via GitHub.remotes
} package.install.packages("remotes")
::install_github("uribo/jpndistrict") remotes
jpndistrict
} package.library(jpndistrict)
→ Extract the map data for Tokyo. → Name it
df_tokyo_map
.
<- jpn_pref(admin_name = "東京都") df_tokyo_map
df_tokyo_map
.class(df_tokyo_map)
[1] "sf" "tbl_df" "tbl" "data.frame"
|>
df_tokyo_map ggplot() +
geom_sf() +
theme_minimal()
<- jpn_pref(admin_name = "東京都") %>%
df_tokyo_map ::filter(str_detect(city, "区")) # Settings to extract only the 23 wards dplyr
|>
df_tokyo_map ggplot() +
geom_sf() +
theme_minimal()
df_tokyo_map
and confirm the
pref_code
for Tokyo.::datatable(df_tokyo_map) DT
pref_code
for Tokyo is 13.<- jpn_pref(13, district = TRUE) %>%
df_tokyo_map ::filter(str_detect(city, "区")) # Settings to extract only the 23 wards dplyr
head(df_tokyo_map, 23)
city
,
geometry
) for drawing the map.<- df_tokyo_map %>%
df_tokyo_map select(city, geometry)
head(df_tokyo_map, 23)
df_tokyo_map
and confirm the
pref_code
for Tokyo.::datatable(df_tokyo_map) DT
pref_code
for Tokyo is 13.<- jpn_pref(13, district = TRUE) %>%
df_tokyo_sf ::filter(str_detect(city, "区")) # Settings to extract only the 23 wards dplyr
%>%
df_tokyo_sf ggplot() +
geom_sf() +
theme_minimal()
head(df_tokyo_sf, 23)
city
,
geometry
) for drawing the map.<- df_tokyo_sf %>%
df_tokyo_sf select(city, geometry)
head(df_tokyo_sf, 23)
e-Stat
(Government Statistics Comprehensive
Portal) and manually download the data.e-Stat
and obtain an “Application ID” to
automatically download the data.市町村データ
」(City and Town Data)
→ 「データ表示
」(Display Data)→ Click「ダウンロード (Download)」
→ Open the downloaded CSV file.
「調査年」 | → year |
「地域」 | → city |
「A1101_総人口【人】」 | → population |
東京都
」 (Tokyo and a half-width space) from
within city.Replace All
.tokyo_pop.csv
(for example, on the
desktop).CSV UTF-8(コンマ区切り)(.csv)
population data
of the 23 wards of
Tokyo obtained from e-Stat with the map data
obtained in
‘1.1 Obtaining Map Data for the 23 Wards of Tokyo’.tokyo_pop.csv
) with the “map data”
(df_tokyo_sf
).data
.tokyo_pop.csv
file in the data folder, read the
data, and name it df_tokyo_pop
.<- read_csv("data/tokyo_pop.csv") df_tokyo_pop
df_tokyo_pop
).head(df_tokyo_pop)
df_tokyo_sf
) downloaded in
1.1 Obtaining Map Data for the 23 Wards of Tokyo
.head(df_tokyo_sf)
df_tokyo_sf
) and Tokyo’s
population data (df_tokyo_pop
) using the common variable
(city
), and name the resulting dataframe
merge_tokyo
.<- df_tokyo_sf %>%
merge_tokyo left_join(df_tokyo_pop, by = "city") %>%
st_as_sf()
%>%
merge_tokyo head() %>%
::paged_table() rmarkdown
<- merge_tokyo %>%
merge_tokyo select(city, geometry, population)
%>%
merge_tokyo head() %>%
::paged_table() rmarkdown
geometry
population
city
merge_tokyo
), display a
map highlighting the wards with higher populations in darker green.<- merge_tokyo %>%
map_pop_tokyo ggplot() +
geom_sf(aes(fill = population)) +
scale_fill_distiller(name = "人口", # 人口 means population
palette = "Greens", direction = 1) +
theme_map(base_family = "HiraginoSans-W3") +
theme(legend.position = c(.1, -.1),
legend.direction = "horizontal",
legend.title = element_text(size = 15),
legend.text = element_text(size = 15),
legend.key.size = unit(1, "cm"),
legend.key.width = unit(3,"cm")) +
coord_sf(datum = NA)
map_pop_tokyo
・Furthermore, display the names of the 23 wards on the map.
<- merge_tokyo %>%
map_pop_tokyo_text mutate(
text_x = map_dbl(geometry, ~st_centroid(.x)[[1]]),
text_y = map_dbl(geometry, ~st_centroid(.x)[[2]])
%>%
) ggplot() +
geom_sf(aes(fill = population)) +
geom_label(aes(x = text_x, y = text_y, label = city),
size = 1.7, family = "HiraginoSans-W3") +
scale_fill_distiller(name = "人口",
palette = "Greens", direction = 1) +
theme_map(base_family = "HiraginoSans-W3") +
theme(legend.position = c(.8, .05),
legend.title = element_text(size = 10),
legend.text = element_text(size = 5),
legend.key.size = unit(0.5, "cm"),
legend.key.width = unit(1,"cm")) +
coord_sf(datum = NA)
map_pop_tokyo_text
fig
in the R project folder, and then
enter the following command.ggsave("fig/map_pop_tokyo_text.png", map_pop_tokyo_text, width = 13, height = 13)
参考:
拓殖大学政経学部浅野ゼミ生の中山郁弥さんが作成したサイト「2014年衆議院総選挙選挙区比較マップ」が役に立ちます