library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.1 ✔ readr 2.1.6
## ✔ lubridate 1.9.4 ✔ stringr 1.6.0
## ✔ purrr 1.2.0 ✔ tibble 3.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# データの読み込み
# df <- read.csv("data_PC.csv", check.names = FALSE, fileEncoding = "UTF-8")
df <- read.csv(text = csv_text, check.names = FALSE)
colnames(df)[1] <- "地域"
alphabet_candidates <- c("KWM", "SHM", "TMN", "NAS", "NKD", "ITO",
"FRK", "YMG", "MRK", "MTS", "MOR", "OKS", "NOD")
# 合計値で並べ替え用の順序を作成
df_order <- df %>%
mutate(Total_Alphabet = rowSums(select(., all_of(alphabet_candidates)))) %>%
arrange(Total_Alphabet)
df_long <- df %>%
select(地域, all_of(alphabet_candidates)) %>%
pivot_longer(cols = -地域, names_to = "候補者", values_to = "得票数")
df_long$地域 <- factor(df_long$地域, levels = df_order$地域)
# グラフ作成:color と linewidth を追加
ggplot(df_long, aes(x = 得票数, y = 地域, fill = 候補者)) +
geom_bar(stat = "identity", color = "white", linewidth = 0.1) + # 境界線を追加
theme_minimal(base_family="HiraKakuProN-W3") +
labs(title = "候補者の地域別得票数",
subtitle = "合計得票数順",
x = "得票数", y = "地域") +
theme(axis.text.y = element_text(size = 8))

library(tidyverse)
# データの読み込み
df <- read.csv("data_PC.csv", check.names = FALSE, fileEncoding = "UTF-8")
colnames(df)[1] <- "地域"
party_names <- c("日本共産党", "日本維新の会", "無所属連合", "日本保守党",
"立憲民主党", "参政党", "国民民主党", "チームみらい",
"日本誠真会", "社会民主党", "れいわ新選組", "日本改革党",
"自由民主党", "再生の道", "公明党", "NHK党")
# 合計値で並べ替え
df_party_order <- df %>%
mutate(Total_Party = rowSums(select(., all_of(party_names)))) %>%
arrange(Total_Party)
df_party_long <- df %>%
select(地域, all_of(party_names)) %>%
pivot_longer(cols = -地域, names_to = "政党", values_to = "得票数")
df_party_long$地域 <- factor(df_party_long$地域, levels = df_party_order$地域)
# グラフ作成:color と linewidth を追加
ggplot(df_party_long, aes(x = 得票数, y = 地域, fill = 政党)) +
geom_bar(stat = "identity", color = "white", linewidth = 0.1) + # 境界線を追加
theme_minimal(base_family="HiraKakuProN-W3") +
labs(title = "政党別の地域別得票数",
subtitle = "合計得票数順",
x = "得票数", y = "地域", fill = "政党") +
theme(axis.text.y = element_text(size = 8),
legend.position = "bottom") +
guides(fill = guide_legend(ncol = 4))
