ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

自治体データをいろいろ分析

データは、ここから入手。
https://www.e-stat.go.jp/
こんな風な tab 区切りファイルに保存して

区分    都道府県    人口総数    15歳未満人口    15歳未満人口÷人口総数  15~64歳人口    15~64歳人口÷人口総数  65歳以上人口    65歳以上人口÷人口総数
1   北海道  5381733 608296  11.3029762  3190804 59.28952625 1558387 28.95697353
1   青森県  1308265 148208  11.32859168 757867  57.92916573 390940  29.88232506
1   岩手県  1279594 150992  11.79999281 734886  57.4311852  386573  30.21059805
1   宮城県  2333899 286003  12.25430064 1410322 60.42772202 588240  25.2041755
1   秋田県  1023119 106041  10.36448351 565237  55.24645716 343301  33.55435682
...省略...

R に読み込み

setwd("C:/Users/xxxxx")
d <- read.table("都道府県.txt", header=T)
d2 <- read.table("支庁.txt", header=T)
d3 <- read.table("政令指定都市.txt", header=T)
d4 <- read.table("市町村.txt", header=T)
d5 <- read.table("政令指定都市内の区.txt", header=T)

ヒストグラムを描画

都道府県

hist(d$"人口総数")

f:id:fornext1119:20180902183436p:plain

hist(d$"総面積")

f:id:fornext1119:20180902183524p:plain

支庁

hist(d2$"人口総数")

f:id:fornext1119:20180902183651p:plain

hist(d2$"総面積")

f:id:fornext1119:20180902183741p:plain

政令指定都市

hist(d3$"人口総数")

f:id:fornext1119:20180902183843p:plain

hist(d3$"総面積")

f:id:fornext1119:20180902183942p:plain

市町村

hist(d4$"人口総数")

f:id:fornext1119:20180902184023p:plain

hist(d4$"総面積")

f:id:fornext1119:20180902184101p:plain

政令指定都市内の区

hist(d5$"人口総数")

f:id:fornext1119:20180902184139p:plain

hist(d5$"総面積")

f:id:fornext1119:20180902184220p:plain

全部

d6 <- rbind(d,d2,d3,d4,d5)
hist(d6$"人口総数")

f:id:fornext1119:20180902184852p:plain

hist(d6$"総面積")

f:id:fornext1119:20180902184933p:plain

ggplot

# ggplot2 パッケージを使用
library(ggplot2)

# 人口総数のヒストグラムを描画
g <- ggplot(d6)
g <- g + geom_histogram(
    aes(
        x=d6$"人口総数"
    ) 
)
# タイトルを変更
g <- g + labs(title="人口総数の分布")
g <- g + xlab("人口総数")
print(g)

f:id:fornext1119:20180902185613p:plain

# 総面積のヒストグラムを描画
g <- ggplot(d6)
g <- g + geom_histogram(
    aes(
        x=d6$"総面積"
    ) 
)
# タイトルを変更
g <- g + labs(title="総面積の分布")
g <- g + xlab("総面積")
print(g)

f:id:fornext1119:20180902185730p:plain

対数軸

# 人口総数のヒストグラムを描画
g <- ggplot(d6)
g <- g + geom_histogram(
    aes(
        x=d6$"人口総数"
    ) 
)
# 対数軸に
g <- g + scale_x_log10()
g <- g + scale_y_log10()
# タイトルを変更
g <- g + labs(title="人口総数の分布")
g <- g + xlab("人口総数")
print(g)

f:id:fornext1119:20180902190714p:plain

# 総面積のヒストグラムを描画
g <- ggplot(d6)
g <- g + geom_histogram(
    aes(
        x=d6$"総面積"
    ) 
)
# 対数軸に
g <- g + scale_x_log10()
g <- g + scale_y_log10()
# タイトルを変更
g <- g + labs(title="総面積の分布")
g <- g + xlab("総面積")
print(g)

f:id:fornext1119:20180902190808p:plain