데이터 시각화

R을 이용한 2020년 국회의원 선거 시각화

r-code-for-data-analysis 2023. 11. 12. 15:09

datatoys 패키지를 이용하여 2020년 국회의원 선거데이터 분석했다.
이외에도 82개의 데이터가 존재하니 데이터 분석 놀이하기에 가장 좋다. 
 
 
https://github.com/statgarten/datatoys

GitHub - statgarten/datatoys: Let's play with data! We prepared toy data for data newbies.

Let's play with data! We prepared toy data for data newbies. - GitHub - statgarten/datatoys: Let's play with data! We prepared toy data for data newbies.

github.com

 
 
1. 패키지 로드 및 데이터 로드

rm(list=ls())
library(datatoys)
library(tidyverse)
library(gt)

set.seed(1234)

cleaned_data <- datatoys::election2020
glimpse(cleaned_data)
head(cleaned_data)

 
2. 데이터 시각화 (파이차트)

library(showtext)
showtext_auto(enable = TRUE, record = TRUE)

cleaned_data %>% 
  mutate_if(is.character, as.factor) %>% 
  filter(구분 == "비례대표") %>% 
  group_by(정당) %>% 
  summarise(득표수 = sum(득표)) %>% arrange(-득표수) %>% 
  mutate(득표율 = 득표수 / sum(.$득표수)*100) %>% arrange(-득표율) %>% 
  slice(1:25) %>% 
  mutate(정당 = fct_reorder(정당,-득표율)) %>% 
 
  ggplot(aes(x="", y=득표율, fill=정당)) +
  geom_bar(stat="identity", width=1,color="white") +
  coord_polar("y", start=0)+
  theme_void()+
  theme(legend.position="none") +
  geom_text(aes(label = 정당, size = ifelse(득표율 > 3, 득표율, 0)), position = position_stack(vjust = 0.5))+
  #geom_text(aes(x=1,y = 득표율, label = 정당), color = "black", size=6) +
  scale_fill_brewer(palette="Set1")+
  labs(title = "2020년 국회의원 선거 비례대표 분포")

 

 
과연 내년  2024년 선거는 어떤 판도가 될지 궁금하다. 
앞으로 선거 전까지 선거 데이터 분석 및 시각화를 해 볼 예정이다. 

728x90
반응형