
の続きです。
今回は、都道府県全体のデータと県庁所在地だけのデータの比較をしてみます。
グラフで視覚化します。
まずは、pop: 2020年(令和2年)の人口(組替)【人】です。


ピンク色が都道府県全体で、青い色が県庁所在地です。県庁所在地のほうが同じくらいの人口に集中していることがわかります。
このまま、このグラフのコードをコピーペーストして、x = pop のところだけ変更していっても良いのですが、自作関数を作ったほうがいいでしょう。
One of the best ways to improve your reach as a data scientist is to write functions. Functions allow you to automate common tasks in a more powerful and general way than copy-and-pasting. Writing a function has four big advantages over using copy-and-paste:
-
You can give a function an evocative name that makes your code easier to understand.
-
As requirements change, you only need to update code in one place, instead of many.
-
You eliminate the chance of making incidental mistakes when you copy and paste (i.e. updating a variable name in one place, but not in another).
-
It makes it easier to reuse work from project-to-project, increasing your productivity over time.
A good rule of thumb is to consider writing a function whenever you’ve copied and pasted a block of code more than twice (i.e. you now have three copies of the same code).
(出典: 25 Functions – R for Data Science (2e))
とあります。
2回以上コピーペーストをするときは、関数を作ることを考慮しましょう、って書いてありますね。
早速作ってみました。

この独自関数を使って他の変数の分布を比較します。


世帯数も人口と同じ傾向です。
次は、setai2: 2020年(令和2年)の世帯数(組替)【世帯】です。


setai2 も pop, setai と同じ分布形状ですね。でも、この(組替)ってなんですかね?
Copilotに聞いたところ、市町村などの境界線が前回の調査と変更があったときの調整、組替だそうです。
次に進みましょう。次は、pop_net: 5年間の人口増減数【人】 です。


青い色、県庁所在地のほうが分布は右にあります。
次は、pop_pct: 5年間の人口増減率【%】です。
![]()

両方ともマイナスの分布が多いですが、県庁所在地のほうがプラスのエリアが大きいです。
次は、set_net: 5年間の世帯数増減【世帯】の分布です。
![]()

なんか、面白い分布ですね。
次は、set_pct: 5年間の世帯増減率【%】の分布です。


世帯数は人口とは違い、増えているエリアが大きいですね。
次は、gender: 人口性比 の分布です。


県庁所在地も都道府県全体も山の峰が2つある分布です。
次は、area: 面積(参考)【km2】の分布です。


県庁所在地のほうが、集中した分布です。
最後は、mitsu: 人口密度【1km2当たり】 の分布です。


人口密度は、都道府県全体のほうが集中しています。
以上、都道府県全体のデータと県庁所在地だけのデータをグラフで比較してみました。
人口や世帯の増減率、男女の比率は分析のしがいがありそうです。
今回は以上です。
初めから読むには、
です。
今回のコードは以下になります。
#
# pop: 2020年(令和2年)の人口(組替)【人】の分布
df |>
ggplot(aes(x = pop, fill = as.factor(city))) +
geom_density(aes(group = city), alpha = 0.5) +
theme_minimal()
#
# 比較用の独自関数を作成
hikaku_graph <- function(df, x_var, fill_var) {
df |>
mutate({{fill_var}} := as.factor({{fill_var}})) |>
ggplot(aes(x = {{x_var}}, fill = {{fill_var}})) +
geom_density(aes(group = {{fill_var}}), alpha = 0.5) +
theme_minimal()
}
#
# setai: 世帯数【世帯】の分布
hikaku_graph(df, setai, city)
#
# setai2: 2020年(令和2年)の世帯数(組替)【世帯】 の分布
hikaku_graph(df, setai2, city)
#
# pop_net: 5年間の人口増減数【人】 の分布
hikaku_graph(df, pop_net, city)
#
# pop_pct: 5年間の人口増減率【%】 の分布
hikaku_graph(df, pop_pct, city)
#
# set_net: 5年間の世帯増減数【世帯】の分布
hikaku_graph(df, set_net, city)
#
# set_pct: 5年間の世帯増減率【%】の分布
hikaku_graph(df, set_pct, city)
#
# gender: 人口性比 の分布
hikaku_graph(df, gender, city)
#
# area: 面積(参考)【km2】の分布
hikaku_graph(df, area, city)
#
# mitsu: 人口密度【1km2当たり】 の分布
hikaku_graph(df, mitsu, city)
#
(冒頭の画像は、Bing Image creator (DALL E-3) で生成しました。プロンプトは、Long wide view of natural grass field, under the blue sky and a few white small clouds, there are very beautiful Amaryllis flowers, close up of a red Amaryllis flower, photo です。)








































