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