R packageslibrary(DT)
library(gapminder)
library(gghighlight)
library(ggrepel)
library(stargazer)
library(tidyverse)boxplot)
は連続変数の分布を示す方法の一つ| 変数 | 可視化の方法 | 特徴 |
|---|---|---|
| 連続変数 | 箱ひげ図 | ①中央値・四分位範囲・最小値・最大値情報が瞬時にわかる |
| ②グループの分布を1つのプロットの中で比較できる | ||
ldp) を作るdf を使って、得票率 (voteshare)
の箱ひげ図を描いてみるgeom_boxplot()y にも x にもマッピングできるパイプ演算子(%>% と
|>) ・2021年5月リリースされた
R 4.1以降、R
内蔵演算子としてパイプ演算子(|>)が追加
・パイプ演算子は %>%
でも |>
でも、どちらを使っても結果は同じ
df %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
ggplot() +
geom_boxplot(aes(y = voteshare, x = seito)) +
labs(x = "政党", y = "得票率") +
ggtitle("政党ごとの得票率:2021年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3")color = ""aes() の「内側に」 color = "skyblue"
のように指定できるdf %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito),
color = "skyblue") +
labs(x = "政党", y = "得票率") +
ggtitle("政党ごとの得票率:2021年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3")fill = ""aes() の「外側に」 fill = "skyblue"
のように指定するcolor = "skyblue"
とマッピングすると、箱内の色でなく、枠線の色が変更されるdf %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito),
fill = "skyblue") +
labs(x = "政党", y = "得票率") +
ggtitle("政党ごとの得票率:2021年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3")scale_fill_manual()
レイヤーを追加するdf %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito,
fill = seito)) +
labs(x = "政党", y = "得票率") +
ggtitle("政党ごとの得票率:2021年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3") +
theme(legend.position = "none")scale_color_manual()
を使うdf %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito,
color = seito)) +
labs(x = "政党", y = "得票率") +
ggtitle("政党ごとの得票率:2021年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3") +
theme(legend.position = "none") +
scale_color_manual(values = c("N党" = "grey",
"れい" = "grey",
"公明" = "red",
"共産" = "grey",
"国民" = "grey",
"無所" = "grey",
"社民" = "grey",
"立憲" = "grey",
"維新" = "grey",
"自民" = "red",
"諸派" = "grey"))gender) を追加してみるaes() の「内側に」 fill = "gender"
のように指定するdf %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito,
fill = gender)) +
labs(x = "政党", y = "得票率") +
ggtitle("政党別・男女別の得票率:2021年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3") +
theme(legend.position = "bottom")aes() の外側に
width を指定するdf %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito),
width = 0.25) + # 幅を 0.25 に設定
labs(x = "政党", y = "得票率") +
ggtitle("政党ごとの得票率:2021年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3")df %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito)) +
labs(x = "政党", y = "得票率") +
ggtitle("政党ごとの得票率:2021年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3")scale_y_discrete(limits = rev)df %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
ggplot() +
geom_boxplot(aes(y = seito,
x = voteshare)) +
labs(x = "政党", y = "得票率") +
theme_bw(base_family = "HiraKakuProN-W3") +
ggtitle("政党ごとの得票率:2021年総選挙") +
scale_y_discrete(limits = rev)ave_vs_2021)
を計算して、中身を確認するdf |>
filter(year == 2021) |>
group_by(seito) |>
summarize(ave_vs_2021 = mean(voteshare)) |>
DT::datatable()ggplot() に渡す前に seito を
factor 化して、順番を決めるseito を factor 化して図を描いてみるdf %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
mutate(seito = factor(seito,
levels = c("公明",
"自民",
"立憲",
"国民",
"維新",
"社民",
"無所",
"共産",
"れい",
"諸派",
"N党"))) %>% # N党の「N」は全角英字
ggplot() +
geom_boxplot(aes(y = seito,
x = voteshare)) +
labs(x = "政党", y = "得票率") +
ggtitle("政党ごとの得票率:2021年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3") 平均値 (mean) と中央値
(median)
・箱ひげ図内の太い線は「中央値 (median)」を表している
・ここでは政党の位置は「平均値 (mean)」を基準として並べている
→ 平均値 (mean) と中央値 (median)は必ずしも一致するとは限らない
Q3.1:「2.2
箱ひげ図の色を変える」の分析手法を使い、2024年総選挙における得票率を政党ごとに箱ひげ図を使って描きなさい
・その際、政党ごとに色をカスタマイズし、民主党候補者を
blue、自民党候補者を red それ以外の政党を grey にすること
Q3.2: 「2.4.4
政党名の表示順番をカスタマイズしたい場合」の分析手法を使い、2024年総選挙における得票率を政党ごとに箱ひげ図を使って描きなさい
・その際、箱ひげ図の上から得票率の高い順に白黒で表示しなさい
Q3.3: 「2.4.4 政党名の表示順番をカスタマイズしたい場合」の分析手法を使い、2024年総選挙において最も得票率の高かった民主党候補者の氏名・選挙区・年齢・得票率・票数の一覧を表示しなさい