library(tidyverse)
library(patchwork)
library(readxl)
# グラフのテーマ
theme_set(theme_classic(base_family = "IPAexGothic", base_size = 16))
# ファイルのダウンロード先ディレクトリ作成
dir.create("files", showWarnings = F)
# 国立社会保障・人口問題研究所のホームページからデータをダウンロード
download.file(
"https://www.ipss.go.jp/site-ad/TopPageData/pyramidDataPP2023J_11.xlsx",
destfile = "files/pyramidDataPP2023J_11.xlsx",
method = "curl"
)
# データの整理 年,年齢階級,性別,人口のlong形式に
<- read_excel(
male "files/pyramidDataPP2023J_11.xlsx",
sheet = "M",
range = "B3:W110",
col_names = TRUE
)
<- read_excel(
female "files/pyramidDataPP2023J_11.xlsx",
sheet = "F",
range = "B3:W110",
col_names = TRUE
)
<- c("total", 0:105)
age
<- cbind(age, male) |>
male pivot_longer(- age, names_to = c("year")) |>
filter(age != "total") |>
mutate(
gender = "M",
age = as.numeric(age),
value = -value
)
<- cbind(age, female) |>
female pivot_longer(- age, names_to = c("year")) |>
filter(age != "total") |>
mutate(
gender = "F",
age = as.numeric(age)
)
<- rbind(male, female)
population
# 人口ピラミッドを作成する年を指定
<- c(1965, 1980, 1995, 2010, 2040, 2070)
years
# 人口ピラミッドの描画
for (i in years) {
<- population |>
fig filter(year == i) |>
ggplot(aes(x = age, y = value, fill = gender)) +
geom_bar(stat = "identity", color = "black", linewidth = 0.1) +
scale_x_continuous(n.breaks = 10) +
scale_y_continuous(
limits = c(-1250, 1250),
breaks = seq(-1000, 1000, 500),
labels = abs(seq(-1000, 1000, 500))
+
) scale_fill_hue(
name = "",
labels = c("F" = "女", "M" = "男")
+
) labs(title = i, y = "", x = "") +
coord_flip() +
theme_classic() +
theme(legend.position = "none")
if (i == years[1]) {
<- fig
g else {
} <- g + fig
g
}
}
plot(g)
人口ピラミッドの推移
概要
人口プラミッドの推移を描く。データは国立社会保障・人口問題研究所のホームページから取得(1965年から2070年の予測まで,Excelファイルで提供されている)。
- 人口ピラミッドの作り方については,人口ピラミッドの作成 (2020年国勢調査)を参照。
- 複数年の人口ピラミッドのグラフをきれいに並べるために,patchworkパッケージを使う。