R Packages we use in this sectionlibrary(DT)
library(gapminder)
library(gghighlight)
library(ggrepel)
library(stargazer)
library(tidyverse)| Variable Type | Visualization Method | Features |
| Continuous | Boxplot | ① Provides instant information about median, quartile range, minimum, and maximum values |
| ② Allows comparison of distributions across groups within a single plot. | ||
Source:浅野・矢内『Rによる計量政治学』p.107.
x [1] 10 7 9 1 0 2 5 8 3 4 6
x in ascending order [1] 0 1 2 3 4 5 6 7 8 9 10
x using
Base Rdfdf, let’s create a boxplot for the vote share
(voteshare).geom_boxplot().y and x.The pipe operator: %>% or
|> ・After the release of R 4.1 in
May 2021, the pipe operator |> was added as an in-built
operator in R.
・The pipe operator can be used as
%>% or |>,
and the results are the same regardless of which one you use.
df %>%
filter(year == 2021) %>% # Narrowing down to 2021 general election data
ggplot() +
geom_boxplot(aes(y = voteshare, x = seito)) +
labs(x = "Party", y = "Voteshare") +
ggtitle("Vote Share by Party: 2021 General Election") +
theme_bw(base_family = "HiraKakuProN-W3")color = ""color = "skyblue" within
aes()df %>%
filter(year == 2021) %>% # Narrowing down to 2021 general election
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito),
color = "skyblue") +
labs(x = "Party", y = "Voteshare") +
ggtitle("Vote Share by Party: 2021 General Election") +
theme_bw(base_family = "HiraKakuProN-W3")fill = ""fill = "skyblue" outside of
aes().color = "skyblue", it changes the color of
the box outline, not the color inside the box.df %>%
filter(year == 2021) %>% # Narrowing down to 2021 general election
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito),
fill = "skyblue") +
labs(x = "Party", y = "Voteshare") +
ggtitle("Vote Share by Party: 2021 General Election") +
theme_bw(base_family = "HiraKakuProN-W3")scale_fill_manual() layer.df %>%
filter(year == 2021) %>% # Narrowing down to 2021 general election
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito,
fill = seito)) +
labs(x = "Party", y = "Voteshare") +
ggtitle("Vote Share by Party: 2021 General Election") +
theme_bw(base_family = "HiraKakuProN-W3")scale_color_manual().df %>%
filter(year == 2021) %>%# Narrowing down to 2021 general election
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito,
color = seito)) +
labs(x = "Party", y = "Voteshare") +
ggtitle("Vote Share by Party: 2021 General Election") +
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"))aes() like
fill = "gender".df %>%
filter(year == 2021) %>% # 2021年総選挙データだけに絞る
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito,
fill = gender)) +
labs(x = "Party", y = "Voteshare") +
ggtitle("Vote Share by Gender: 2021 General Election") +
theme_bw(base_family = "HiraKakuProN-W3") +
theme(legend.position = "bottom")aes().df %>%
filter(year == 2021) %>% # Focus only on the 2021 general election data
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito),
width = 0.25) + # Set width to 0.25
labs(x = "Party", y = "Vote Share") +
ggtitle("Vote Share by Party: 2021 General Election") +
theme_bw(base_family = "HiraKakuProN-W3")df %>%
filter(year == 2021) %>% # Focus only on the 2021 general election data
ggplot() +
geom_boxplot(aes(y = voteshare,
x = seito)) +
labs(x = "Party", y = "Vote Share") +
ggtitle("Vote Share by Party: 2021 General Election") +
theme_bw(base_family = "HiraKakuProN-W3")df %>%
filter(year == 2021) %>% # Focus only on the 2021 general election data
ggplot() +
geom_boxplot(aes(y = seito,
x = voteshare)) +
labs(x = "Party", y = "Vote Share") +
ggtitle("Vote Share by Party: 2021 General Election") +
theme_bw(base_family = "HiraKakuProN-W3")scale_y_discrete(limits = rev)df %>%
filter(year == 2021) %>% # Focus only on the 2021 general election data
ggplot() +
geom_boxplot(aes(y = seito,
x = voteshare)) +
labs(x = "Party", y = "Vote Share") +
theme_bw(base_family = "HiraKakuProN-W3") +
ggtitle("Vote Share by Party: 2021 General Election") +
scale_y_discrete(limits = rev)df |>
filter(year == 2021) |>
group_by(seito) |>
summarize(ave_sd_2021 = sd(voteshare)) |>
DT::datatable()df %>%
filter(year == 2021) %>% # Focus only on the 2021 general election data
filter(voteshare > 75) %>% # Filter only candidates with a vote share over 75%
select(pref, kun, seito, age, voteshare, vote, j_name) %>%
head(10)# A tibble: 10 × 7
pref kun seito age voteshare vote j_name
<chr> <dbl> <chr> <dbl> <dbl> <dbl> <chr>
1 宮崎 3 自民 56 80.7 111845 古川禎久
2 宮城 6 自民 61 83.2 119555 小野寺五典
3 群馬 5 自民 47 76.6 125702 小渕優子
4 広島 1 自民 64 80.7 133704 岸田文雄
5 香川 3 自民 53 79.8 94437 大野敬太郎
6 山口 2 自民 62 76.9 109914 岸信夫
7 山口 3 自民 60 76.9 96983 林芳正
8 秋田 3 自民 57 78.0 134734 御法川信英
9 石川 2 自民 47 78.4 137032 佐々木紀
10 鳥取 1 自民 64 84.1 105441 石破茂
fill = seito as a mapping element, it can be
displayed in color.df %>%
filter(year == 2021) %>% # Focus only on the 2021 general election data
mutate(seito = factor(seito,
levels = c("公明",
"自民",
"立憲",
"国民",
"維新",
"社民",
"れい",
"共産",
"無所",
"N党",
"諸派"))) %>% # N党の「N」は全角英字
ggplot() +
geom_boxplot(aes(x = voteshare,
y = seito,
fill = seito)) +
labs(x = "Party", y = "Vote Share") +
ggtitle("Vote Share by Party: 2021 General Election") +
theme_bw(base_family = "HiraKakuProN-W3") +
theme(legend.position = "none")