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)2.2 Changing the Color of Box Plots, draw a box plot for
the 2009 general election vote share for each political party.2.4.4 Customizing the Order of Party Names Display, draw a
box plot for the 2009 general election vote share for each political
party. Display the box plots in black and white, in order of highest
vote share from the top.2.4.4 Customizing the Order of Party Names Display, display
a list of the names, electoral districts, ages, vote shares, and vote
counts of the Democratic Party candidates with the highest vote share in
the 2009 general election.