産業構造・職業構成。
利用するデータ:
/Users/moteki/rstudio2022/data/職業分類.csv
以下は統計表ファイルの中の記述。
第7表 職業(大分類), 男女別15歳以上就業者数-全国(平成7年~27年)
1)総務省統計局において,平成17年国勢調査新職業分類特別集計のデータを用いて,新旧分類間の分割比率を算出して推計した。
2)平成7年~17年は,一部の調査票を抽出して集計した抽出詳細集計に基づいて推計,集計しており,基本集計(全ての調査票を用いた集計)とは一致しない。
3)平成17年国勢調査新職業分類特別集計結果による。
統計表における項目名の行は外して読み込み用ファイルとしたので、以下でそれに合わせて、列名は読みこまないという指示(col_names = FALSE)をした。
library(readr)
職業分類 <- read_csv("./data/職業分類.csv",
col_names = FALSE) # 最初の行からデータ
work <- 職業分類
# 列名の指定
colnames(work) <- c("code","職業","1995","2000","2005","2010","2015")
# 保存
work0 <- work
データフレームの行名をアルファベットの大文字にするためには、以下のようにする。小文字を生成する場合にはletters[]を使う。
work <- as.data.frame(work)
rownames(work) <- LETTERS[1:12]
棒グラフをbarplotで描く。帯グラフとするためにhoriz=Tとした。
(正方形のグラフとする場合にはpty=“s”と設定する。)
fig.width=6,fig.asp=0.8,out.width="70%",fig.align="center"
par(family="HiraKakuProN-W3")
barplot(dat,horiz=T,legend.text = T,xlab="percent",ylab="year",names=c("1995","2000","2005","2010","2015"),col=topo.colors(12),main = "職業構成の変化\n1995-2015",args.legend = list(x = "bottomleft"))
ggplot2を使う。保存したwork0を呼び出してデータを編集する。ここでは、データの1列目の職業コードを表示用に使う。 long formatに変換したものをdat_とする。
# 最初から12行目までの男女計のデータを取り出す。
# 2列目の日本語の職業分類を外す。1列目のcodeは残す。
work <- work0[1:12,-2]
library(tidyr)
# long formatに変換(オブジェクト名: work_)
dat_ <- gather(work,"key","value",-code)
library(ggplot2)
library(RColorBrewer)
ggplot(dat_,aes(x=key,y=value,fill=code))+
geom_col()+
labs(x="year",y="percent",fill="legend",title="職業構成の変化",caption = "国勢調査",subtitle = "1995-2015")+
scale_fill_brewer(palette = "Paired")+
theme_minimal(base_family = "HiraKakuProN-W3")
次は、coord_flipで向きを変える。
次のグラフでは、左の方は凡例(legend)を省略した。
fig.width=6,fig.asp=0.8,out.width="70%",fig.align="default"
## Warning: The `guide` argument in `scale_*()` cannot be `FALSE`. This was deprecated in
## ggplot2 3.3.4.
## ℹ Please use "none" instead.
| code | 職業 |
|---|---|
| A | 管理的職業従事者 |
| B | 専門的・技術的職業従事者 |
| C | 事務従事者 |
| D | 販売従事者 |
| E | サービス職業従事者 |
| F | 保安職業従事者 |
| G | 農林漁業従事者 |
| H | 生産工程従事者 |
| I | 輸送・機械運転従事者 |
| J | 建設・採掘従事者 |
| K | 運搬・清掃・包装等従事者 |
| L | 分類不能の職業 |
work <- work0[c(13,14,25,26),-2]
work
## # A tibble: 4 × 6
## code `1995` `2000` `2005` `2010` `2015`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 A 6.4 4.4 3.7 3.6 3.5
## 2 B 12.3 13 12.6 13.4 14.7
## 3 A 1 0.8 0.7 0.8 0.9
## 4 B 12.5 13.4 14.6 15.9 17.5
2つの職業を男女で比較する。
最初のグラフのコードは次の通り。グラフ中にtext(6.5,5.5,"3.5%",col = "blue")として注釈を入れている。
work$code <- c("管理的職業(男)","専門的・技術的職業(男)","管理的職業(女)","専門的・技術的職業(女)")
MA <- work[1,-1]
par(family="HiraKakuProN-W3",mfrow=c(1,2))
barplot(as.matrix(MA),horiz = T,col = "grey",main = "管理的職業従事者(男)",xlab = "パーセント",xlim=c(0,20))
par(new=T)
text(6.5,5.5,"3.5%",col = "blue")
long formatでデータを作成。
dat_ <- gather(work,"key","value",-code)
ggplot(dat_,aes(x=key,y=value,group=code))+
geom_line(size=2,aes(col=code))+
labs(x="year",y="percent",fill="legend",title="専門的管理的職業の動向",caption = "国勢調査",subtitle = "1995-2015")+
guides(color=guide_legend(reverse = TRUE))+
theme_minimal(base_family = "HiraKakuProN-W3")
ggplotのaesの中でgroup=codeとし、また、geom_lineのaesの中でcol=codeと設定している。size=2は、aesの外で設定している。
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
ファイル名: lt05-02
[基本集計] 長期時系列表5 (3)産業(第10回改定分類)別就業者数 - 全国
[Basic Tabulation] Historical data 5 (3)Employed person by industry (10th revision industrial classification) - Whole Japan
(万人) (Ten thousand persons)
男女計 Both sexes
総数 農林業 非農林業
Total, Agriculture and forestry, Non-agricultural industries
漁業 鉱業 建設業 製造業 電気・ガス・熱供給・水道業 運輸・通信業
卸売・小売業,飲食店 金融・保険業,不動産業 サービス業 公務 (他に分類されないもの)
Fisheries
Mining
Construction
Manufacturing
Electricity, gas, heat supply and water
Transport and communicati- on
Wholesale and retail trade, eating and drinking places
Financing and insurance, real estate
Services
Government (not elsewhere classified)
library(readr)
長期時系列表5 <- read_csv("./data/長期時系列表5.csv")
sector <- 長期時系列表5
sector[,c(1,2,3)]
## # A tibble: 36 × 3
## ...1 ...2 ...3
## <chr> <chr> <dbl>
## 1 43 (1968)* 5002
## 2 44 (1969)* 5040
## 3 45 (1970)* 5094
## 4 46 (1971)* 5121
## 5 47 (1972)* 5126
## 6 48 (1973)* 5223
## 7 48 (1973) 5259
## 8 49 (1974) 5237
## 9 50 (1975) 5223
## 10 51 (1976) 5271
## # … with 26 more rows
最初の2列は、年次。元号と西暦。
6列目、7列目は、1973年。 西暦を入れ直すとそれ以降1年ずつずれるので、6行目をカットしてから西暦を ふりなおす。
colnames(sector) <- c("year1","year2","01T","02農林業","03NA","04漁業","05鉱業","06建設業","07製造業","08電気等","09運輸・通信業","10卸売・小売業、飲食店","11金融・保険","12サービス業","13G")
sector$year1 <- 1968:2003
sctr <- sector[-6,-c(3,5,15)]
sctr$year1 <- 1968:2002
sctr[,1:2]
## # A tibble: 35 × 2
## year1 year2
## <int> <chr>
## 1 1968 (1968)*
## 2 1969 (1969)*
## 3 1970 (1970)*
## 4 1971 (1971)*
## 5 1972 (1972)*
## 6 1973 (1973)
## 7 1974 (1974)
## 8 1975 (1975)
## 9 1976 (1976)
## 10 1977 (1977)
## # … with 25 more rows
sctr0 <- sctr # データの保存
colnames(sctr0)
## [1] "year1" "year2" "02農林業"
## [4] "04漁業" "05鉱業" "06建設業"
## [7] "07製造業" "08電気等" "09運輸・通信業"
## [10] "10卸売・小売業、飲食店" "11金融・保険" "12サービス業"
01総数
02農林業
03非農林業
04漁業
05鉱業
06建設業
07製造業
08電気・ガス・熱供給・水道業
09運輸・通信業
10卸売・小売業,飲食店
11金融・保険業,不動産業
12サービス業
13公務 (他に分類されないもの)
[1] "year1" "year2"
[3] "02農林業" "04漁業"
[5] "05鉱業" "06建設業"
[7] "07製造業" "08電気等"
[9] "09運輸・通信業" "10卸売・小売業、飲食店"
[11] "11金融・保険" "12サービス業"
上記を参照してとりあげる変数を選ぶ。(4列目以降は数字が一致。)
sctr <- sctr[,c(1,3,6,7,9,10,12)]
dat_ <- gather(sctr,"key","value",-year1)
library(readr)
industry2009 <- read_csv("./data/産業別就業人口2009以降2.csv")
sector <- industry2009
# colnames(sector) <- paste("X",seq(18))
colnames(sector) <- c("year","02農業,林業","03非農林業","04建設業","05製造業",
"06情報通信業",
"07運輸業,郵便業",
"08卸売業,小売業",
"09金融業,保険業",
"10不動産業,物品賃貸業",
"11学術研究,専門・技術\nサービス業",
"12宿泊業,\n飲食サービス業",
"13生活関連サービス業,\n娯楽業",
"14教育,学習支援業",
"15医療,福祉",
"16複合サービス事業",
"17サービス業\n(他に分類されないもの)",
"18公務")
sector0 <- sector # データの保存
sector <- sector[,c(1,6,7,8,9,10,11,12,13,14,15,17,18)]
sector$year <- as.character(sector$year)
## # A tibble: 6 × 3
## year key value
## <chr> <chr> <dbl>
## 1 2009 06情報通信業 194
## 2 2010 06情報通信業 197
## 3 2011 06情報通信業 191
## 4 2012 06情報通信業 188
## 5 2013 06情報通信業 192
## 6 2014 06情報通信業 204
## # A tibble: 6 × 3
## year key value
## <chr> <chr> <dbl>
## 1 2014 18公務 235
## 2 2015 18公務 231
## 3 2016 18公務 231
## 4 2017 18公務 229
## 5 2018 18公務 232
## 6 2019 18公務 241
[詳細集計] 長期時系列表9 (1)年齢階級(10歳階級)別就業者数及び年齢階級(10歳階級),雇用形態別雇用者数 - 全国 (男女計)
[Detailed Tabulation] Historical data 9 (1) Employed person by age group (ten-year group) and Employee by age group (ten-year group) and type of employment - Whole Japan (Both sexes)
library(readr)
雇用形態別雇用者数 <- read_csv("./data/雇用形態別雇用者数.csv")
status <- 雇用形態別雇用者数
stts <- status[,c(5,6)]
status[c(20,40,60),c(1,5,6)]
## # A tibble: 3 × 3
## ...1 `Regular employee` `Non-regular employee`
## <chr> <dbl> <dbl>
## 1 2006 Oct.-Dec. average 3448 1693
## 2 2011 Oct.-Dec. average 3328 1845
## 3 2016 Oct.-Dec. average 3383 2050
stts$index <- 1:74
colnames(stts)[1:2] <- c("正規の職員・従業員","非正規の職員・従業員")
library(tidyr)
dat_ <- gather(stts,"key","value",-index)
library(ggplot2)
library(RColorBrewer)
ggplot(dat_,aes(x=index,y=value,group=key))+
geom_line(size=1,aes(col=key))+
labs(x="期別",y="雇用者数",title="雇用形態別雇用者数の推移",caption = "長期時系列表9",subtitle = "2002年1-3月期より2020年4-6月期まで\n(単位: 万人)",col="凡例")+
guides(color=guide_legend(reverse = FALSE))+
scale_color_brewer(palette = "Paired")+
xlim(1,74)+
annotate("text",x=20,y=2500,label="2006年10-12月期",angle=90,color="grey60",family="HiraKakuProN-W3")+
annotate("text",x=40,y=2500,label="2011年10-12月期",angle=90,color="grey60",family="HiraKakuProN-W3")+
annotate("text",x=60,y=2800,label="2016年10-12月期",angle=90,color="grey60",family="HiraKakuProN-W3")+
theme_minimal(base_family = "HiraKakuProN-W3")
ファイル名:05420
第20表年齢階級別非正規の職員・従業員の割合
男女
a.総数
b.15-64
c.15-24
d.25-34
e.35-44
f.45-54
g.55-64
h.65ー
男
a.総数
b.15-64
c.15-24
d.25-34
e.35-44
f.45-54
g.55-64
h.55-59
i.60-64
j.65ー
女
(同上)
library(readr)
年齢別非正規割合 <- read_csv("./data/年齢別非正規割合.csv")
dat <- 年齢別非正規割合
dat$X1 <- 2013:2019
colnames(dat)[2:9] <- letters[1:8]
colnames(dat)[10:19] <- letters[1:10]
colnames(dat)[20:29] <- letters[1:10]
# 最初の列は、年号である。
# 3つのオブジェクトに分ける。男女計(a)、男(m)、女(f)。
dat_a <- dat[,1:9]
dat_m <- dat[,c(1,10:19)]
dat_f <- dat[,c(1,20:29)]
dat_a <- as.matrix(dat_a)
dat_m <- as.matrix(dat_m)
dat_f <- as.matrix(dat_f)
dat
## # A tibble: 7 × 30
## ...1 a b c d e f g h a b c d
## <lgl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NA 36.6 34.6 48.7 27.3 28.9 32.2 47.8 71.5 21.1 18.2 45.1 16.3
## 2 NA 37.4 35.1 48.2 27.9 29.6 32.7 48.3 73.1 21.7 18.4 43.8 16.8
## 3 NA 37.4 34.7 47.8 27.2 29.6 32.6 47.4 74.2 21.8 18 43.9 16.5
## 4 NA 37.5 34.4 47.6 26.3 29.3 32.4 47.3 75.1 22.1 17.9 44.4 15.8
## 5 NA 37.2 34.1 47.3 25.8 28.6 32.3 47.1 74.4 21.8 17.5 44.4 15.2
## 6 NA 37.8 34.3 49.8 24.9 28.8 32.1 46.9 76.3 22.2 17.4 46.2 14.3
## 7 NA 38.2 34.4 50.4 24.7 28.7 32.1 46.6 77.3 22.8 17.6 46.8 14.5
## # … with 17 more variables: e <dbl>, f <dbl>, g <dbl>, h <dbl>, i <dbl>,
## # j <dbl>, a <dbl>, b <dbl>, c <dbl>, d <dbl>, e <dbl>, f <dbl>, g <dbl>,
## # h <dbl>, i <dbl>, j <dbl>, X1 <int>
2019年のデータ(7行目)だけを取り出す。
dat_a_2019 <- dat_a[7,4:9]
dat_a_2019
## c d e f g h
## 50.4 24.7 28.7 32.1 46.6 77.3
dat_m_2019 <- dat_m[7,c(4:7,9:11)]
dat_m_2019
## c d e f h i j
## 46.8 14.5 9.3 8.7 11.2 49.6 73.3
dat_f_2019 <- dat_f[7,c(4:7,9:11)]
dat_f_2019
## c d e f h i j
## 54.1 37.0 51.6 57.7 61.0 76.9 82.0
表示のための準備。行の併合、行名、列名の指定。
dat_mf_2019 <- rbind(dat_m_2019,dat_f_2019) # 行の併合
rownames(dat_mf_2019) <- c("M","F")
colnames(dat_mf_2019) <- c("15-24","25-34","35-44","45-54","55-59","60-64","65-")
dotchartによる描画。
aspを1.0に設定している。縦を少し長くしている。
fig.width=6,fig.asp=1.0,out.width="70%",fig.align="center"
par(family="HiraKakuProN-W3")
dotchart(dat_mf_2019,cex=0.8,xlab = "パーセント",main = "非正規職員・従業員の割合: 2019年")
dotchart(t(dat_mf_2019),xlab = "パーセント",main = "非正規職員・従業員の割合: 2019年")
列(group)の挿入。 long formatへの変換。
library(tidyr)
dt <- as.data.frame(dat_mf_2019)
dt
## 15-24 25-34 35-44 45-54 55-59 60-64 65-
## M 46.8 14.5 9.3 8.7 11.2 49.6 73.3
## F 54.1 37.0 51.6 57.7 61.0 76.9 82.0
dt$sex <- c("M","F")
dat_ <- gather(dt,"key","value",-sex)
dat_
## sex key value
## 1 M 15-24 46.8
## 2 F 15-24 54.1
## 3 M 25-34 14.5
## 4 F 25-34 37.0
## 5 M 35-44 9.3
## 6 F 35-44 51.6
## 7 M 45-54 8.7
## 8 F 45-54 57.7
## 9 M 55-59 11.2
## 10 F 55-59 61.0
## 11 M 60-64 49.6
## 12 F 60-64 76.9
## 13 M 65- 73.3
## 14 F 65- 82.0
ggplot2による描画。 凡例のタイトルはlabsの中でcolor=“legend”と指定することができる。これをしないと、long formatのinndex変数として使用したsexが表示される。
library(patchwork)
p1 <- ggplot(dat_,aes(x=key,y=value),group=sex)+
geom_point(aes(col=sex),size=2)+
labs(x="age", y="percent",title="Non-regular employees",subtitle = "2019")+
theme(legend.position = "none")
p2 <- ggplot(dat_,aes(x=key,y=value),group=sex)+
geom_point(aes(col=sex),size=2)+
guides(color=guide_legend(reverse = T))+
labs(x="age", y="percent",title="",subtitle = "",color="legend")+
coord_flip()
次のグラフの設定は以下の通り。
fig.width=7,fig.asp=0.618,out.width="70%",fig.align="default"
以下の表は、カテゴリカルな軸の要素の順番を変更する方法などを確認できる。
ggplot(dat_,aes(x=key,y=value),group=sex)+
geom_col(aes(fill=sex),size=3,position = "dodge")+
guides(fill=guide_legend(reverse = T))+
labs(x="age", y="percent",title="Non-regular employees",subtitle = "2019")
棒グラフの場合、以下のように2カ所でcolorをfillに変える必要があった。2番目は、凡例の項目順をM,Fの順にするために。
geom_col(aes(fill=sex),size=3)
guides(fill=guide_legend(reverse = T))
ggplot(dat_,aes(x=key,y=value),group=sex)+
geom_col(aes(fill=sex),size=3,position = "dodge")+
guides(fill=guide_legend(reverse = T))+
labs(x="age", y="percent",title="Non-regular employees",subtitle = "2019")+
coord_flip()
ggplot(dat_,aes(x=key,y=value),group=sex)+
geom_col(aes(fill=sex),size=3,position = "dodge")+
guides(fill=guide_legend(reverse = T))+
labs(x="age", y="percent",title="Non-regular employees",subtitle = "2019")+
scale_x_discrete(limits=c("65-","60-64","55-59","45-54","35-44","25-34","15-24")) +
coord_flip()
表の転置。年齢をindexにする。
dt
## 15-24 25-34 35-44 45-54 55-59 60-64 65- sex
## M 46.8 14.5 9.3 8.7 11.2 49.6 73.3 M
## F 54.1 37.0 51.6 57.7 61.0 76.9 82.0 F
dt <- as.data.frame(t(dt[,-8]))
dt
## M F
## 15-24 46.8 54.1
## 25-34 14.5 37.0
## 35-44 9.3 51.6
## 45-54 8.7 57.7
## 55-59 11.2 61.0
## 60-64 49.6 76.9
## 65- 73.3 82.0
name <- rownames(dt)
dt$age <- letters[1:7]
dt
## M F age
## 15-24 46.8 54.1 a
## 25-34 14.5 37.0 b
## 35-44 9.3 51.6 c
## 45-54 8.7 57.7 d
## 55-59 11.2 61.0 e
## 60-64 49.6 76.9 f
## 65- 73.3 82.0 g
dat_ <- gather(dt,"key","value",-age)
dat_
## age key value
## 1 a M 46.8
## 2 b M 14.5
## 3 c M 9.3
## 4 d M 8.7
## 5 e M 11.2
## 6 f M 49.6
## 7 g M 73.3
## 8 a F 54.1
## 9 b F 37.0
## 10 c F 51.6
## 11 d F 57.7
## 12 e F 61.0
## 13 f F 76.9
## 14 g F 82.0
dat_$age <- name
dat_
## age key value
## 1 15-24 M 46.8
## 2 25-34 M 14.5
## 3 35-44 M 9.3
## 4 45-54 M 8.7
## 5 55-59 M 11.2
## 6 60-64 M 49.6
## 7 65- M 73.3
## 8 15-24 F 54.1
## 9 25-34 F 37.0
## 10 35-44 F 51.6
## 11 45-54 F 57.7
## 12 55-59 F 61.0
## 13 60-64 F 76.9
## 14 65- F 82.0
次のグラフの設定は以下の通り。
fig.width=6,fig.asp=0.618,out.width="70%",fig.align="center"
library(ggplot2)
ggplot(dat_,aes(x=age,y=value,group=key))+
geom_line(size=2,aes(col=key))+
#geom_point(col="grey")+
labs(x="age",y="percent",title="Non-regular employees",caption = "Table 20",subtitle = "2019",col="legend")+
guides(color=guide_legend(reverse = TRUE))+
theme_minimal(base_family = "HiraKakuProN-W3")