R packageslibrary(DT)
library(gapminder)
library(gghighlight)
library(ggrepel)
library(stargazer)
library(tidyverse)bar chart)
は離散変数の分布を示す方法の一つ| 変数 | 可視化の方法 | 特徴 |
|---|---|---|
| 離散変数 | 棒グラフ | 棒の間の線あり |
| 変数 | 可視化の方法 | 特徴 |
|---|---|---|
| 離散変数 | 棒グラフ | 棒の間の線あり |
| 連続変数 | ヒストグラム | 棒の間の線なし |
衆院選データ
(hr96-26.csv)の準備
・hr96-26.csv
をクリックしてデータをパソコンにダウンロード
・選挙データの読み取り
hr96_17.csv
は1996年に衆院選挙に小選挙区が導入されて以来実施された 11
回の衆議院選挙(1996, 2000, 2003, 2005, 2009, 2012, 2014, 2017, 2021,
2024, 2026)の結果のデータdf に含まれる変数名を表示させる [1] "year" "pref" "ku" "kun"
[5] "j_name" "wl" "rank" "nocand"
[9] "seito" "gender" "name" "previous"
[13] "age" "exp" "status" "vote"
[17] "voteshare" "eligible" "turnout" "seshu_dummy"
[21] "jiban_seshu" "nojiban_seshu"
df には 22 個の変数が入っている| 変数名 | 詳細 |
|---|---|
| 1. year | 選挙年 (1996-2026) |
| 2. pref | 都道府県名 |
| 3. ku | 小選挙区名 |
| 4. kun | 小選挙区 |
| 5. wl | 選挙の当落: 1 = 小選挙区当選、2 = 復活当選、0 = 落選 |
| 6. rank | 当選順位 |
| 7. nocand | 立候補者数 |
| 8. seito | 候補者の所属政党 |
| 9. j_name | 候補者の氏名(日本語) |
| 10. gender | 立候補者の性別: “male”, “female” |
| 11. name | 候補者の氏名(ローマ字) |
| 12. previous | これまでの当選回数(当該総選挙結果は含まない) |
| 13. age | 立候補者の年齢 |
| 14. exp | 立候補者が使った選挙費用(総務省届け出) |
| 15. status | 候補者のステータス: 0 = 非現職、1 現職、2 = 元職 |
| 16. vote | 得票数 |
| 17. voteshare | 得票率 (%) |
| 18. eligible | 小選挙区の有権者数 |
| 19. turnout | 小選挙区の投票率 (%) |
| 20. seshu_dummy | 世襲候補者ダミー: 1 = 世襲、0 = 非世襲(地盤世襲 or 非世襲) |
| 21. jiban_seshu | 地盤の受け継ぎ元の政治家の氏名と関係 |
| 22. nojiban_seshu | 世襲元の政治家の氏名と関係 |
df に含まれる変数の型 (class)
をチェックspc_tbl_ [11,892 × 22] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ year : num [1:11892] 1996 1996 1996 1996 1996 ...
$ pref : chr [1:11892] "愛知" "愛知" "愛知" "愛知" ...
$ ku : chr [1:11892] "aichi" "aichi" "aichi" "aichi" ...
$ kun : num [1:11892] 1 1 1 1 1 1 1 2 2 2 ...
$ j_name : chr [1:11892] "伊東マサコ" "河村たかし" "岩中美保子" "今枝敬雄" ...
$ wl : num [1:11892] 0 1 0 0 0 0 0 2 0 1 ...
$ rank : num [1:11892] 5 1 4 2 3 6 7 3 8 1 ...
$ nocand : num [1:11892] 7 7 7 7 7 7 7 8 8 8 ...
$ seito : chr [1:11892] "文化フォーラム" "新進" "共産" "自民" ...
$ gender : chr [1:11892] "female" "male" "female" "male" ...
$ name : chr [1:11892] "ITO, MASAKO" "KAWAMURA, TAKASHI" "IWANAKA, MIHOKO" "IMAEDA, NORIO" ...
$ previous : num [1:11892] 0 2 0 2 2 0 0 0 0 2 ...
$ age : num [1:11892] 51 47 43 72 53 51 45 30 43 51 ...
$ exp : num [1:11892] NA 9828097 2177203 9311555 9231284 ...
$ status : num [1:11892] 0 1 0 2 1 0 0 0 0 1 ...
$ vote : num [1:11892] 616 66876 22209 42969 33503 ...
$ voteshare : num [1:11892] 0.4 40 13.3 25.7 20.1 0.3 0.2 25.7 0.2 32.9 ...
$ eligible : num [1:11892] 346774 346774 346774 346774 346774 ...
$ turnout : num [1:11892] 49.2 49.2 49.2 49.2 49.2 49.2 49.2 51.8 51.8 51.8 ...
$ seshu_dummy : num [1:11892] 0 0 0 0 0 0 0 0 0 0 ...
$ jiban_seshu : chr [1:11892] NA NA NA NA ...
$ nojiban_seshu: chr [1:11892] NA NA NA NA ...
- attr(*, "spec")=
.. cols(
.. year = col_double(),
.. pref = col_character(),
.. ku = col_character(),
.. kun = col_double(),
.. j_name = col_character(),
.. wl = col_double(),
.. rank = col_double(),
.. nocand = col_double(),
.. seito = col_character(),
.. gender = col_character(),
.. name = col_character(),
.. previous = col_double(),
.. age = col_double(),
.. exp = col_double(),
.. status = col_double(),
.. vote = col_double(),
.. voteshare = col_double(),
.. eligible = col_double(),
.. turnout = col_double(),
.. seshu_dummy = col_double(),
.. jiban_seshu = col_character(),
.. nojiban_seshu = col_character()
.. )
- attr(*, "problems")=<externalptr>
chr、数値型は
num で問題なしdf) の変数の記述統計を表示させる{r, results = "asis"}
と指定する| Statistic | N | Mean | St. Dev. | Min | Max |
| year | 11,892 | 2,011.09 | 9.63 | 1,996 | 2,026 |
| kun | 11,892 | 5.90 | 5.33 | 1 | 30 |
| wl | 11,892 | 0.48 | 0.67 | 0 | 2 |
| rank | 11,892 | 2.48 | 1.28 | 1 | 9 |
| nocand | 11,892 | 3.96 | 1.12 | 2 | 9 |
| previous | 11,892 | 1.53 | 2.54 | 0 | 19 |
| age | 11,888 | 51.62 | 11.30 | 25 | 94 |
| exp | 9,479 | 7,125,574.00 | 5,201,119.00 | 535 | 27,462,362 |
| status | 11,892 | 0.49 | 0.62 | 0 | 2 |
| vote | 11,892 | 54,787.54 | 39,815.58 | 177 | 210,515 |
| voteshare | 11,892 | 27.34 | 19.21 | 0.10 | 95.30 |
| eligible | 10,773 | 351,536.30 | 66,499.14 | 191,241 | 495,212 |
| turnout | 10,773 | 59.75 | 7.10 | 42.09 | 83.80 |
| seshu_dummy | 11,892 | 0.09 | 0.29 | 0 | 1 |
ggplot() と
geom_bar()| 幾何オブジェクト | 意味 |
ggplot() |
図を描くキャンバスを用意する |
geom_bar() |
棒(棒グラフ)を描く |
df を使う table()
関数を使って、出馬した立候補者数を確かめる
1996 2000 2003 2005 2009 2012 2014 2017 2021 2024 2026
1261 1199 1026 989 1139 1294 959 936 857 1113 1119
df の中には「総選挙年」を表す変数 year
はあるgeom_bar() の aes()
内に指定するマッピングは、棒の横軸上の位置を意味する x
だけで良いy は
ggplot2 が勝手に計算し表示してくれるgeom_bar(aes(x = year)) だけで良いlabs() レイヤーを使うと、x 軸と
y 軸のラベルを指定できるdf %>%
ggplot() +
geom_bar(aes(x = year)) +
labs(x = "総選挙年", y = "立候補者総数") +
ggtitle("立候補者数(小選挙区:1996-2026年総選挙)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策year の class が
numeric だからstr() 関数を使って year の
class を確認 num [1:11892] 1996 1996 1996 1996 1996 ...
year の class は
numeric(数値)R は year を連続変数だと認識する解決策:
year の class を
numeric(数値)から factor に変更するyear の class を確認 Factor w/ 11 levels "1996","2000",..: 1 1 1 1 1 1 1 1 1 1 ...
year の class は factor
に変更されたdf %>%
ggplot() +
geom_bar(aes(x = year)) +
labs(x = "総選挙年",
y = "立候補者総数") +
ggtitle("平均立候補者数(小選挙区:1996-2026年総選挙)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策bar_plot1
など)のオブジェクトとして格納できる「5 グラフの保存方法」を参照bar_plot1 <- df %>%
ggplot() +
geom_bar(aes(x = year)) +
labs(x = "総選挙年",
y = "立候補者総数")+
ggtitle("立候補者数(小選挙区:1996-2026年総選挙)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策bar_plot1) 入力すればよいこのグラフからわかること ・総選挙における立候補者総数は、1996年以降(2009年と2012年を除き)2021年までは減少傾向だったが、2024年と2026年に増加傾向にある
df を使う df の中には「総選挙年」を表す変数 year
はあるgroup_by()
関数を使って「総選挙・小選挙区ごとの立候補者数の平均」を計算し、ave_nocand1
として格納する# A tibble: 3,251 × 4
# Groups: year, ku [517]
year ku kun ave_nocand1
<fct> <chr> <dbl> <dbl>
1 1996 aichi 1 7
2 1996 aichi 2 8
3 1996 aichi 3 7
4 1996 aichi 4 6
5 1996 aichi 5 7
6 1996 aichi 6 8
7 1996 aichi 7 7
8 1996 aichi 8 5
9 1996 aichi 9 7
10 1996 aichi 10 7
# ℹ 3,241 more rows
ave_nocand1
を使って総選挙ごとの平均を求め 、ave_nocand2 として格納する
# A tibble: 11 × 2
year ave_nocand2
<fct> <dbl>
1 1996 4.20
2 2000 4.00
3 2003 3.42
4 2005 3.30
5 2009 3.81
6 2012 4.31
7 2014 3.28
8 2017 3.24
9 2021 2.99
10 2024 3.85
11 2026 3.87
geom_bar() の aes()
内に指定するマッピング:x = yeary = ave_nocand2geom_bar(aes(x = year, y = ave_nocand2)) と指定x と y
の両方を指定する場合、geom_bar()内側、かつ
aes() の外側に stat = "identity"
と指定するlabs() レイヤーを使うと、x 軸と
y 軸のラベルを指定できるdf1 %>%
ggplot() +
geom_bar(aes(x = year,
y = ave_nocand2),
stat = "identity") +
labs(x = "総選挙年", y = "小選挙区ごとの立候補者数の平均") +
ggtitle("平均立候補者数(小選挙区:1996-2026年総選挙)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策このグラフからわかること ・総選挙における立候補者総数は、2021年までは減少傾向にあったが、2024年と2026年には増加傾向にある
status)N)df を使う status = 0 は新人、status = 1 は現職、
status = 2 は元職table()
関数を使って、出馬した立候補者数を確かめる
0 1 2
6808 4315 769
status)
を横軸、「立候補者数」(N) を縦軸に指定df の中には「現職・新人・元職の分類」を表す変数
status はあるN がないgeom_bar() の aes()
内に指定するマッピングは、棒の横軸上の位置を意味する x
だけで良いy(つまり「立候補者数
N」)は ggplot2
が勝手に計算し表示してくれるgeom_bar(aes(x = year)) と指定するだけで良いlabs() レイヤーを使うと、x 軸と
y 軸のラベルを指定できるggtitle()
レイヤーを使うと、メインタイトルを付けることができるdf %>%
ggplot() +
geom_bar(aes(x = status)) +
labs(x = "現職・新人・元職の分類", y = "立候補者総数") +
ggtitle("現職・新人・元職別の立候補者数(1996-2026年総選挙)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策case_when() 関数を使って df の変数
status を日本語にリコーディングするstatus_J という新たな変数として追加し、データフレーム
df2 に格納するx = status の代わりに x = status_J
と指定すれば良いdf2 %>%
ggplot() +
geom_bar(aes(x = status_J)) +
labs(x = "現職・新人・元職の分類",
y = "立候補者総数") +
ggtitle("現職・新人・元職別の立候補者数(1996-2026年総選挙)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策グラフ内の文字化け対策
Windows
ユーザーの場合「文字化け対策」として次のコマンドを打つこと
theme_bw(base_family = "HiraKakuProN-W3")
Mac
ユーザーの場合「文字化け対策」として次のコマンドを打つこと
theme_bw(base_family = "HiraKakuProN-W3")
factor() 関数 status_J を
factor 化するdf2 を ggplot() 関数に渡す前に
mutate() 関数 を入れ、そこで status_J 変数を
factor 化するlevels = c("現職", "新人", "元職")df2 %>%
mutate(status_J = factor(status_J,
levels = c("現職", "新人", "元職"))) %>%
ggplot() +
geom_bar(aes(x = status_J)) +
labs(x = "現職・新人・元職の分類",
y = "立候補者総数") +
ggtitle("現職・新人・元職別の立候補者数(1996-2026年総選挙)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策fct_inorder() 関数 forcats パッケージに含まれている forcats パッケージは tidyverse
パッケージに含まれている tidyverse
パッケージを読み込んだら別途読み込む必要はないfct_inorder() 関数は () 内の変数を
factor 化しdf2 の status_J
の値(=要素)がどのような順番で登場しているか確認してみる [1] "新人" "現職" "新人" "元職" "現職" "新人"
status_J
の値(=要素)は「現職・元職・新人」の順番で登場しているstatus_J
の値の順番が固定したい時に fct_inorder()
関数を使う df2 %>%
mutate(status_J = fct_inorder(status_J)) %>%
ggplot() +
geom_bar(aes(x = status_J)) +
labs(x = "現職・新人・元職の分類", y = "立候補者総数") +
ggtitle("現職・新人・元職別の立候補者数(1996-2026年総選挙)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策x 軸と y
軸のみで構成される 2 次元グラフx 軸) の立候補者数
(y 軸)」といった 2 つの情報が含まれるggplot2 の幾何オブジェクト内の
aes() 内で指定する「引数の数」のことseito
に属する立候補者数の棒グラフを作ってみるdata.frame を df_24
として用意する
みらい れいわ 中道 保守 共産 参政 国民 減ゆ 無所 社民 維新
6 18 202 6 158 182 102 13 41 8 87
自民 諸派
285 11
seito を factor
化する
自民 中道 参政 共産 国民 維新 無所 れいわ 減ゆ 諸派 社民
285 202 182 158 102 87 41 18 13 11 8
みらい 保守
6 6
棒を表示する順番を指定する方法
・例えば、政党 (seito)
を自民、立憲、共産という順番で表示したけければ、変数をファクター化する際に順番を指定する
mutate(seito = factor(seito,
levels = c("自民", "立憲", "共産")))
N を計算(性別・政党別・当落別)df26 を使って性別と政党別に立候補者数 N
を計算res_1 と名前を付けるres_1 %>%
ggplot() +
geom_bar(aes(x = seito,
y = N),
stat = "identity") +
ggtitle("2026年総選挙での立候補者数(政党別)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策x で指定した所属政党 seitoy で指定した立候補者数 Nfill = gender上で作成した図に、もう一つの次元として「候補者の性別」gender
を加える
次元を追加するときには aes()
内に引数を追加する
「棒」は点・線・面のうち、面に該当
面には次の要素がある:
・面の色(fill)
・枠線の色(color)
・枠線のタイプ(linetype)
・透明度(alpha)など
もう一つの次元を追加するのであれば、識別する定番の方法は色
棒グラフの場合、「面の色」と「枠線の色」をそれぞれ指定可能
ここでは面の色(fill)にマッピングする
aes()内に fill = gender
を追加する
res_1 %>%
ggplot() +
geom_bar(aes(x = seito,
y = N,
fill = gender),
stat = "identity") +
ggtitle("2026年総選挙での立候補者数(政党別・性別)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策position = "dodge"dodge) みるgeom_bar() 内に position = "dodge"
を指定するaes() の「中」でなく、「外」に指定することres_1 %>%
ggplot() +
geom_bar(aes(x = seito,
y = N,
fill = gender),
stat = "identity",
position = "dodge") + # position = "dodge" を aes() の「外」に指定
ggtitle("2026年総選挙での立候補者数(政党別・性別)") +
theme_bw(base_family = "HiraKakuProN-W3") # 文字化け対策theme(legend.position = "bottom")theme()theme() 内に
legend.position = "bottom" を指定"right"("top", "left", "none"
が指定可能)res_1 %>%
ggplot() +
geom_bar(aes(x = seito,
y = N,
fill = gender),
stat = "identity",
position = "dodge") +
ggtitle("2026年総選挙での立候補者数(政党別・性別)") +
theme_bw(base_family = "HiraKakuProN-W3") + # 文字化け対策
theme(legend.position = "bottom") facet)分割は、色を使った分類を避けたい時に効果的facet_wrap(~ 分割の基準となる変数名)
のレイヤーを加えるres_1 %>%
ggplot() +
geom_bar(aes(x = gender,
y = N),
stat = "identity") +
ggtitle("2026年総選挙での立候補者数(政党別・性別)") +
theme_bw(base_family = "HiraKakuProN-W3") + # 文字化け対策
facet_wrap(~ seito) # 分割の基準は seito と指定res_1 %>%
ggplot() +
geom_bar(aes(x = seito,
y = N),
stat = "identity") +
ggtitle("2026年総選挙での立候補者数(政党別・性別)") +
theme_bw(base_family = "HiraKakuProN-W3") + # 文字化け対策
facet_wrap(~ gender) # 分割の基準は seito と指定theme(axis.text.x = element_text(angle = XX, vjust = X, hjust = X)x
軸のラベルが長くて、左右でオーバーラップすることがよくある(ここでは大丈夫)x
軸のラベルを回転させると読みやすくなるtheme()
レイヤーを追加し、axis.text.x を指定するangle
だけを修正するres_1 %>%
ggplot() +
geom_bar(aes(x = seito,
y = N),
stat = "identity") +
facet_wrap(~ gender) + # 分割の基準は seito と指定
labs(x = "政党", y = "立候補者数") + # ラベル修正
ggtitle("2026年総選挙での立候補者数(政党別・性別)") +
theme_minimal() + # テーマを変えてみよう
theme_bw(base_family = "HiraKakuProN-W3") + # 文字化け対策
theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust = 1),
panel.grid.major.x = element_blank()) aes() の内側に fill = 変数
と指定することで、新たなレイヤーを色別して追加できるelec_result
という変数を作ってから、ggplot() に渡すres_1 %>%
mutate(elec_result = if_else(wl == "0", "落選", "当選")) %>%
ggplot() +
geom_bar(aes(x = seito,
y = N,
fill = elec_result), # 新たなレイヤー elec_result の追加
stat = "identity") +
facet_wrap(~ gender) + # 分割の基準は gender と指定
labs(x = "政党", y = "立候補者数") + # ラベル修正
ggtitle("2026年総選挙での立候補者数(政党別・小選挙区当落別)") +
theme_minimal() + # テーマを変えてみよう
theme_bw(base_family = "HiraKakuProN-W3") + # 文字化け対策
theme(axis.text.x = element_text(angle = 40, # 40度回転
vjust = 1,
hjust = 1),
panel.grid.major.x = element_blank()) + # 縦線グリッドを削除
theme(legend.position = "bottom") # レジェンドの位置指定x 軸と y 軸を入れ替えるx 軸と y 軸を入れ替える方法x 軸のラベルが長くて左右オーバーラップする場合x 軸と y 軸を入れ替えるres_1 %>%
ggplot() +
geom_bar(aes(x = N,
y = seito),
stat = "identity") +
facet_wrap(~ gender) + # 分割の基準は gender と指定
labs(x = "立候補者数", y = "政党") + # ラベル修正
ggtitle("2026年総選挙での立候補者数(政党別・性別)") +
theme_minimal() + # テーマを変えてみよう
theme_bw(base_family = "HiraKakuProN-W3") geom_text(aes(label = 表示させたい内容))
を指定するres_1 の中身を確認するres_1を作った課程を確認する
df26 <- df %>%
filter(year == 2026) %>% # 2026年総選挙データだけに絞り df24 と名前を付ける
drop_na(seito) %>% # seito の欠損値を除外する
mutate(seito = factor(seito, # seito をファクター化し政党名の表示順序を指定
levels = c("自民",
"中道",
"参政",
"共産",
"国民",
"維新",
"無所",
"れいわ",
"減ゆ",
"諸派",
"社民",
"保守",
"みらい")))res_1 <- df26 %>% # 政党別、性別、当落別に立候補者数 N を計算し、res_1と名前と付ける
group_by(seito, gender, wl) %>%
summarise(N = n()) res_1 の内容を表示させるres_1 を使って 11
の「政党それぞれの立候補者総数」= party_cand
を計算res_2 と名付けるres_2 <- res_1 %>%
group_by(seito) %>%
summarize(party_cand = sum(N)) # 性別と政党ごとに N を合計し total と名前と付け res_2 に格納する res_2 の内容を表示させるgeom_text(aes(label = party_cand)
を使って数値を表示させる res_2 %>%
ggplot(aes(x = seito,
y = party_cand)) +
geom_bar(aes(),
stat = "identity",
position = position_dodge(width = 0.9)) +
geom_text(aes(label = party_cand),
vjust = 1.2,
colour = "yellow",
position = position_dodge(width = 0.9),
size = 3) +
theme_bw(base_family = "HiraKakuProN-W3") +
ggtitle("2026年総選挙での立候補者数(政党別)") +
theme(legend.position = "bottom") 表示させる数値は全部で 22
それらは 2026 年における 13 の政党それぞれの男女別の立候補者数
res_1 の内容を表示させる
res_1 を使って 13
の「政党それぞれの男女別の立候補者総数」= total
を計算res_3 と名付けるres_3 <- res_1 %>%
group_by(seito, gender) %>%
summarize(total = sum(N)) # 性別と政党ごとに N を合計し total と名前と付け res_2 に格納する res_3 の内容を表示させるgeom_text(aes(label = total)
を使って数値を表示させる res_3 %>%
ggplot(aes(x = seito,
y = total,
fill = gender)) +
geom_bar(aes(),
stat = "identity",
position = position_dodge(width = 0.9)) +
geom_text(aes(label = total),
vjust = 1.2,
colour = "white",
position = position_dodge(width = 0.9),
size = 3) +
theme_bw(base_family = "HiraKakuProN-W3") +
ggtitle("2026年総選挙での立候補者数(政党別・性別)") +
theme(legend.position = "bottom") vjust = 数値)
を取り去りcolour = "black"res_3 %>%
ggplot(aes(x = seito,
y = total,
fill = gender)) +
geom_bar(aes(),
stat = "identity",
position = position_dodge(width = 0.9)) +
geom_text(aes(label = total),
colour = "black", # 表示させる数字の色を黒に変更
position = position_dodge(width = 0.9),
size = 3) +
theme_bw(base_family = "HiraKakuProN-W3") +
ggtitle("2026年総選挙での立候補者数(政党別・性別)") +
theme(legend.position = "bottom") position = "stack" と指定表示させる数値は全部で 22
それらは 2026 年における 13 の政党それぞれの男女別の立候補者数
res_1 を使って 11
の「政党それぞれの男女別の立候補者総数」= total
を計算
→ 結果を res_4 と名付ける
res_4 の内容を表示させるgeom_text(aes(label = total)
を使って数値を表示させる res_4 %>%
ggplot(aes(x = seito,
y = total,
fill = gender)) +
geom_bar(aes(),
stat = "identity",
position = "stack") + # position = "stack" に変更
geom_text(aes(label = total),
colour = "black",
position = "stack", # position = "stack" に変更
size = 3) +
theme_bw(base_family = "HiraKakuProN-W3") +
ggtitle("2026年総選挙での立候補者数(政党別・性別)") +
theme(legend.position = "bottom") | 画像のフォーマット | 拡張子 | ファイルサイズ | 画質 |
|---|---|---|---|
| ベクトル画像 | .pdf(推奨) .svg |
大きい | 拡大しても綺麗 |
| ビットマップ画像 | .png(推奨) .bmp .jpg .jpeg |
小さい | そこそこ綺麗 |
bar_plot_1 を高解像度の
.png 形式で保存してみるggplot2 で作図した図は ggsave()
関数で保存するbar_plot1
と付けた)RProject フォルダ内に図を保存するためのフォルダ
fig を作るknit するggsave(filename = "fig/plot1.png", # 保存先とファイル名
plot = bar_plot1, # 保存する図のオブジェクト名
width = 6, # 図の幅 (インチ)
height = 3, # 図の高さ (インチ)
dpi = 400, # 解像度
device = ragg::agg_png) # 文字化け防止bar_plot1
というオブジェクト名を付けたRProject フォルダー内に Figs
という名前を付けたフォルダを作成bar_plot1 に plot1.png
という名を付けて を保存しているragg
パッケージは、図を保存する際に文字化けを防いでくれるQ6.1:
「3.1 シンプルな棒グラフ(1)」を参考にして、1996年から2026年総選挙で立候補した男性の候補者数を棒グラフで描きなさい
Q6.2:
「3.1 シンプルな棒グラフ(1)」を参考にして、1996年から2026年総選挙で立候補した女性の候補者数を棒グラフで描きなさい
Q6.3:
「3.5.3 棒を左右にずらす」を参考にして、1996年から2026年総選挙で立候補した男性と女性の候補者数を棒グラフで描きなさい
Q6.4(難):
「3.8 ファセットに新たなレイヤーを追加」を参考にして、2026年総選挙において政党ごとの現職・新人・元職の当選者数を棒グラフで描きなさい
・その際、当選者(小選挙区当選者 +
復活当選者)と落選者を色分けして表示しなさい
Q6.5(難):
「4.2 ずらした棒グラフの場合」を参考にして、2026年総選挙における当選者数(小選挙区・比例復活当選)と落選者数を棒グラフで描きなさい
・その際、当選者と落選者を色分けして表示し、それぞれの人数を棒グラフの内側(もしくはその付近)に表示させなさい