R packageslibrary(DT)
library(gapminder)
library(gghighlight)
library(ggrepel)
library(stargazer)
library(tidyverse)scatter plot) は 2
つの連続変数(=間隔尺度、比率尺度で測定された変数)間の関係を調べる代表的な可視化方法ggplot() と
geom_point()| 幾何オブジェクト | 意味 |
ggplot() |
図を描くキャンバスを用意する |
geom_point() |
散布図を描く |
1996-2021年総選挙データから、2005年データを抜き出し「選挙費用」と「得票率」の散布図を描いてみる
必要なデータは次の 2 つ:
exp)voteshare)df09 と名前を付けるx に、縦軸上の位置は y
にマッピングするx 軸に設定(ここでは「選挙費用
exp」)y 軸に設定(ここでは「得票率
voteshare」)exp,
votehsarex 軸と y 軸にラベルを付けるggtitle() 関数を使ってメインタイトルを付けるdf05 %>%
ggplot() +
geom_point(aes(x = exp,
y = voteshare),
color = "royalblue") +
labs(x = "選挙費用",
y = "得票率") +
ggtitle("選挙費用と得票率の散布図: 2005年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3")shape = 番号 でドットの形を指定できる△) に指定してみるdf05 %>%
ggplot() +
geom_point(aes(x = exp,
y = voteshare),
color = "royalblue",
shape = 2) +
labs(x = "選挙費用",
y = "得票率") +
ggtitle("選挙費用と得票率の散布図: 2005年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3")●」0 〜 14 の場合・・・中身が透明で枠線のみの形color 引数で調整15 〜 20 の場合・・・中身が埋まり枠線のない形15 〜 25 の場合・・・枠線は
color、内側の色塗りは fill で調整shape = 22
を使い「内側がバイオレット」で「枠線がアクアマリン」の「□」を表示させてみる df05 %>%
ggplot() +
geom_point(aes(x = exp,
y = voteshare),
color = "violet", # 枠線の色を指定
fill = "aquamarine", # 内側の色塗り指定
shape = 22) + # ドットの形を指定
labs(x = "選挙費用",
y = "得票率") +
ggtitle("選挙費用と得票率の散布図: 2005年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3")exp と votehsare
という 2 つの情報 (= 2 次元) をもつif_else() 関数を使って自民党ダミー
ldp を作成し、散布図に次元を追加してみるaes() 関数の内側に shape = ldp
と指定してみるaes() の内側に shape = ldp と指定df05 %>%
mutate(dpj = if_else(seito == "自民", "自民党", "非自民党")) %>%
ggplot() +
geom_point(aes(x = exp,
y = voteshare,
shape = dpj)) + # dpj をドットの形で区別
labs(x = "選挙費用",
y = "得票率") +
ggtitle("選挙費用と得票率の散布図: 2005年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3")shape = ldp
と指定すると、Rが自動的に「●」と「▲」を割り当てる○」(空っぽのマル)×」scale_shape_manual() を使うdf05 %>%
mutate(dpj = if_else(seito == "自民", "自民党", "非自民党")) %>%
ggplot() +
geom_point(aes(x = exp,
y = voteshare,
shape = dpj)) + # dpj をドットの形で区別
labs(x = "選挙費用",
y = "得票率") +
ggtitle("選挙費用と得票率の散布図: 2005年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3") +
theme(legend.position = "bottom") + # レジェンドの位置を下に
scale_shape_manual(values = c("自民党" = 1, # 「○」は 1
"非自民党" = 4)) # 「×」は 4 ●」と「▲」よりずっと見やすくなったaes() 関数の内側に color = ldp
と指定してみるdf05 %>%
mutate(dpj = if_else(seito == "自民", "自民党", "非自民党")) %>%
ggplot() +
geom_point(aes(x = exp,
y = voteshare,
color = dpj, # dpj を色分け
alpha = 0.5)) + # 透明度を追加
labs(x = "選挙費用",
y = "得票率") +
ggtitle("選挙費用と得票率の散布図: 2005年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3") +
theme(legend.position = "bottom") # レジェンドの位置を下にscale_color_manual() レイヤーを追加valuesc("値1" = "色1", "値2" = "色2", ...)
のように名前を付けcharacter 型ベクトルを指定df05 %>%
mutate(dpj = if_else(seito == "自民", "自民党", "非自民党")) %>%
ggplot() +
geom_point(aes(x = exp,
y = voteshare,
color = dpj, # dpj を色分け
alpha = 0.5)) + # 透明度を追加
labs(x = "選挙費用",
y = "得票率") +
ggtitle("選挙費用と得票率の散布図: 2005年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3") +
theme(legend.position = "bottom") + # レジェンドの位置を下に
scale_color_manual(values = c("自民党" = "blue",
"非自民党" = "gold"))2005年の小泉郵政選挙では、自民党候補者の方がより多く得票している傾向がよく分かる
ggplot2 で使える色は 657 種類!
"red"、"skyblue"、"royalblue"のように文字で指定できる
Rで使用可能な色のリストはコンソール上で colors()
と打ち込むと確認できる
ここでは最初の 6 色を示す
[1] "white" "aliceblue" "antiquewhite" "antiquewhite1"
[5] "antiquewhite2" "antiquewhite3"
RGBカラー(HEmathコード;
16進数)で指定することもできる"#FF0000"、ロイヤルブルーなら
"#4169E1"と表記geom_smooth(method = lm)
を加えるggplot() の内側に aes()
関数を指定し、そこに x 軸、y
軸、color の設定をするplot_vs_05 <- df05 %>%
ggplot(aes(x = exp,
y = voteshare,
color = seito,
alpha = 0.5)) + # ドットの透明度を指定
geom_point() +
geom_smooth(method = lm) + # 回帰直線を引く
labs(x = "選挙費用",
y = "得票率") +
ggtitle("選挙費用と得票率の散布図: 2005年総選挙") +
theme_bw(base_family = "HiraKakuProN-W3")
plot_vs_05