R packageslibrary(DT)
library(gapminder)
library(gghighlight)
library(ggrepel)
library(stargazer)
library(tidyverse)histogram)
は連続変数の分布を示す方法の一つ| 変数 | 可視化の方法 | 特徴 |
|---|---|---|
| 離散変数 | 棒グラフ | 棒の間の線あり |
| 連続変数 | ヒストグラム | 棒の間の線なし |
ggplot2 では geom_histogram()
を使ってヒストグラムを描くx
にマッピングするだけy に対してマッピングを行う必要はなく、x
のみで描けるvoteshare)
のヒストグラムを描いてみるdf <- read_csv("data/hr96-21.csv",
na = ".") ldp) を作るdf <- df %>%
mutate(ldp = if_else(seito == "自民", "自民党", "非自民党")) # 自民党ダミー (ldp) を作る df %>%
ggplot() +
geom_histogram(aes(x = voteshare)) # votehsare のヒストグラム「幅が狭すぎる」ヒストグラムは、ピークが 2 つあることはわかるが、無駄にギザギザが多い
「幅が広すぎる」ヒストグラムは、ピークがどこにあるかわからない
「幅が適度」ヒストグラムは、ピークが 2 つあることがわかる
どれが「良いヒストグラム」なのか?
「幅が狭広すぎる」ヒストグラムはあまりにも多くの情報が失われている
「幅が狭すぎる」ヒストグラムは情報が多すぎる
→ 「幅が適度」のヒストグラムが適切
どれが望ましいヒストグラムなのかは作図者が決める
棒の数を調整した複数のヒストグラムを出力して比較する
→ 最も望ましいヒストグラムを選ぶ
binsgeom_histogram()
内、aes() の外に bins
引数を指定することで変更可能bins = 5
と指定しているbinwidthgeom_histogram()
内、aes() の外に binwidth
引数を指定することで変更可能binwidth = 5
と指定してみるgeom_histogram() 内、aes()
の外に color 引数を指定"white")に指定しているbin とbinwidth
・棒の「幅」binwidthと棒の「数」binsはどちらか片方のみ指定可能
・両方指定することはできない
hist_plot1
と名前を付けて表示してみるhist_plot1 <- df %>%
ggplot() +
geom_histogram(aes(x = voteshare),
color = "white",
binwidth = 5)
hist_plot1x 軸の表示を変更したいヒストグラム hist_plot1
に、目盛り変更のレイヤを追加してみる
目盛りを変更するレイヤー:
scale_x_*(), scale_y_*()
* の箇所には各軸の尺度を指定
ヒストグラムは横軸も縦軸も「連続」した値 → *
の箇所には continuous が入る
scale_x_continuous() |
x 軸の目盛りを変更 | 位置 | ラベル |
scale_y_continuous() |
y 軸の目盛りを変更 | 位置 | ラベル |
x 軸だけ変更してみる必要な引数:
・目盛りの位置を指定する breaks 引数
・目盛りのラベルを意味する labels 引数
・それぞれ長さ 1 以上のベクトルを指定する
・breaks と labels
に使用する実引数(=ベクトル)の長さは同じ長さ
・labels には character
型ベクトルを指定することもできる
目盛りは 0 から 100 まで 10
間隔で付け、目盛りラベルもそれぞれ対応する値を指定
公差 10 の等差数列ベクトルなので seq()
関数を使う
目盛り調整済みのヒストグラムは hist_plot2
として格納、出力してみる
hist_plot2 <- hist_plot1 +
scale_x_continuous(breaks = seq(0, 100, by = 10),
labels = seq(0, 100, by = 10))
hist_plot2x 軸の表記が指定通りに変更された!facet_wrap(~) を使う場合hist_plot2
のヒストグラムは自民党ダミー(ldp)を使って分ける場合、棒グラフと同様
facet_wrap(~ldp) で分割できるhist_plot2 +
facet_wrap(~ldp) +
theme_bw(base_family = "HiraKakuProN-W3")aes() 内に fill
を指定するposition = "identity"
を追加する必要があるdf %>%
mutate(ldp = if_else(seito == "自民", "自民党", "非自民党")) %>%
ggplot() +
geom_histogram(aes(x = voteshare,
fill = ldp),
position = "identity",
binwidth = 10,
color = "white") +
labs(x = "得票率",
y = "立候補者数",
fill = "") +
theme_bw(base_family = "HiraKakuProN-W3")alpha 引数を使って、棒の透明度を上げるaes() の外側に
alpha を指定alpha = 0・・・透明alpha = 1・・・不透明alpha = 0.5 に指定してみるdf %>%
mutate(ldp = if_else(seito == "自民", "自民党", "非自民党")) %>%
ggplot() +
geom_histogram(aes(x = voteshare,
fill = ldp),
position = "identity",
binwidth = 10,
color = "white",
alpha = 0.5) +
labs(x = "得票率",
y = "立候補者数",
fill = "") +
theme_bw(base_family = "HiraKakuProN-W3")df %>%
ggplot() +
geom_histogram(aes(x = voteshare,
fill = seito),
position = "identity",
binwidth = 10,
color = "white",
alpha = 0.5) +
labs(x = "得票率",
y = "立候補者数",
fill = "") +
theme_bw(base_family = "HiraKakuProN-W3")df %>%
filter(seito == "自民"|seito == "共産"|seito == "民主") %>%
ggplot() +
geom_histogram(aes(x = voteshare,
fill = seito),
position = "identity",
binwidth = 10,
color = "white",
alpha = 0.5) +
labs(x = "得票率",
y = "立候補者数",
fill = "") +
theme_bw(base_family = "HiraKakuProN-W3")position = "identity" の指定をしなけばdf %>%
filter(seito == "自民"|seito == "共産"|seito == "民主") %>%
ggplot() +
geom_histogram(aes(x = voteshare,
fill = seito),
binwidth = 10,
color = "white",
alpha = 0.5) +
labs(x = "得票率",
y = "立候補者数",
fill = "") +
theme_bw(base_family = "HiraKakuProN-W3")geom_histogram() の代わりに geom_density()
を使うdf %>%
filter(seito == "自民"|seito == "共産"|seito == "民主") %>%
ggplot() +
geom_density(aes(x = voteshare,
fill = seito),
color = "white",
alpha = 0.5,
adjust = 0.8) +
labs(x = "得票率",
y = "密度",
fill = "") +
theme_bw(base_family = "HiraKakuProN-W3")binwidth や
bins 引数は不要adjust 引数を使う(デフォルトは 1)adjust の値が小さいほど密度曲線がギザギザadjust の値が小さいほど密度曲線が滑らかcount)でなく、密度(density)になるQ3.1:
「2.4 次元の追加」の分析手法を使い、2021年総選挙における得票率のヒストグラムを自民党・非自民党候補者で色分けして描きなさい
・その際、幾何オブジェクト geom_histogram()
を使うこと
・自民党候補者の棒と非自民党候補者の棒のオーバーラップにも留意して可視化すること
Q3.2:
「2.4 次元の追加」の分析手法を使い、2021年総選挙における得票率のヒストグラムを自民党・立憲民主党・共産党ごとに色分けして描きなさい
・その際、幾何オブジェクト geom_density()
密度曲線を出力すること