knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(krvote)

선거 일정

suppressPackageStartupMessages({
  library(tidyverse)
  library(vistime)
  library(extrafont)
  library(scales)
  library(RColorBrewer)
})

# font_import(paths="C:/Windows/Fonts", prompt = FALSE)

loadfonts(device = "win", quiet = TRUE)

timeline_raw <- data.frame(
  group = c("대통령","대통령","대통령","대통령","대통령","대통령",
            "국회의원","국회의원","국회의원","국회의원","국회의원","국회의원","국회의원",
            "지방선거","지방선거","지방선거","지방선거","지방선거","지방선거"),
  event = c("15대", "16대", "17대", "18대","19대","20대",
            "16대","17대", "18대", "19대", "20대", "21대", "22대",
            "3회","4회","5회","6회","7회","8회"),
  start = c("1997-12-18","2002-12-19","2007-12-19","2012-12-19", "2017-05-10", "2022-05-10",
            "2000-05-30","2004-05-30","2008-05-30", "2012-05-30","2016-05-30","2020-05-30", "2024-05-30",
            "2002-07-01", "2006-07-01", "2010-07-01","2014-07-01","2018-07-01","2022-07-01"),
  end   = c("2002-12-17","2007-12-18","2012-12-18","2017-05-08", "2022-05-09", "2027-05-09",
            "2004-05-29","2008-05-29", "2012-05-29","2016-05-29","2020-05-29","2024-05-29", "2028-05-29",
            "2006-06-30", "2010-06-30", "2014-06-30","2018-06-30","2022-06-30","2026-06-30")) %>% 
  mutate(start = lubridate::ymd(start),
         end   = lubridate::ymd(end)) %>% 
  as_tibble()

color_tbl <- tibble(
  group = c(rep("대통령", 6),
            rep("국회의원", 7),
            rep("지방선거", 6)),
  color = c(brewer.pal(6, "Reds"),
            brewer.pal(7, "Blues"),
            brewer.pal(6, "Greens")))

timeline_df <- timeline_raw %>% 
  mutate(color = color_tbl$color) %>% 
  group_by(group) %>% 
  mutate(color = case_when((group == "국회의원" & start == max(start)) ~ "#1565c0",
                           TRUE ~ color)) %>% 
  mutate(color_text = case_when((group == "국회의원" & start == max(start)) ~ "white",
                                 TRUE ~ "black")) %>%   
  ungroup()

timeline_viz <- gg_vistime(timeline_df,
           title = "대한민국 3대 선거",
           show_labels = TRUE,
           background_lines = NULL,
           optimize_y = TRUE,
           linewidth = 20,
           col.color = "color",
           col.fontcolor = "color_text") +
  theme_bw(base_family = "NanumBarunpen") +
  theme(
    # text = element_text(size = 50),
    plot.title = element_text(size = 25),
    axis.text.x = element_text(size = 15, color = "black"),
    axis.text.y = element_text(size = 15, color = "black")
    # panel.background = element_rect(fill = 'gray85'),
    # panel.border = element_rect(linetype = "dashed", fill=NA)
  ) +
  scale_x_datetime(breaks = scales::breaks_width("5 years"), labels = date_format("%Y")) +
  labs(caption = "제작: 한국 R 사용자회",
       subtitle = "제22대 국회의원 선거") +
  ## 선거일 ----------------
  annotate("point", x = as.POSIXct("2022-03-09"), y = 5.3, size = 2.5, colour = "red") +
  geom_label(aes(x = as.POSIXct("2022-05-17"), y = 5.5, label = "03월09일"), size = 4.5, colour = "red") +
  annotate("point", x = as.POSIXct("2024-04-10"), y = 3.3, size = 2.5, colour = "blue") +
  geom_label(aes(x = as.POSIXct("2024-04-10"), y = 3.5, label = "04월10일"), size = 4.5, colour = "blue") +
  annotate("point", x = as.POSIXct("2022-06-01"), y = 1.3, size = 2.5, colour = "darkgreen") +
  geom_label(aes(x = as.POSIXct("2022-06-01"), y = 1.5, label = "06월01일"), size = 4.5, colour = "darkgreen")+  
  ### 현재일 -------------
  geom_vline(xintercept = as.numeric(as.POSIXct("2022-12-23")), color = "gray", size = 1.0,
             linetype = "dashed") +
  annotate("text", x = as.POSIXct("2022-12-25"), y = 6.5, label = "현재 (12월24)", size = 5, colour = "gray10",
           family = "NanumBarunpen") +
  coord_cartesian(ylim = c(0, 6), clip = "off")  

timeline_viz

# filename <- "../timeline_viz.png"
# ragg::agg_png(filename, width = 297, height = 210, units = "mm", res = 600)
# timeline_viz
# dev.off()


ai-carpentry/krvote documentation built on April 22, 2024, 1 a.m.