###### tags: `건국대` `챗GPT 디지털 글쓰기` `한국R사용자회` # https://bit.ly/3rSCEDi # 오마이뉴스 기사 - https://www.ohmynews.com/NWS_Web/View/at_pg.aspx?CNTN_CD=A0002947326 # 지도 뉴스 - https://r2bit.com/map_challenge/precinct.html # Two table verbs https://statkclee.github.io/data-science/ds-dplyr-join.html # Graph Output ``` ragg::agg_jpeg("data/two_map.jpg", width = 10, height = 7, units = "in", res = 600) nomination_type_final dev.off() ``` # 오늘 실습 코드 ``` --- title: "너도 기자냐 나도 기자다" author: "이광춘" format: html editor: visual editor_options: chunk_output_type: console --- # 데이터 ## 지도 데이터는 오마이뉴스 GitHub에서 가져옵니다. ```{r} library(tidyverse) library(sf) map_sf <- sf::st_read("data/2020_21_elec_253_simple.json") st_geometry(map_sf) |> plot() st_drop_geometry(map_sf) |> as_tibble() ``` ## 후보 데이터 ```{r} #| eval: false library(rvest) wiki_url <- "https://ko.wikipedia.org/wiki/%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD_%EC%A0%9C21%EB%8C%80_%EA%B5%AD%ED%9A%8C%EC%9D%98%EC%9B%90_%EC%84%A0%EA%B1%B0_%EB%8D%94%EB%B6%88%EC%96%B4%EB%AF%BC%EC%A3%BC%EB%8B%B9_%ED%9B%84%EB%B3%B4_%EB%AA%A9%EB%A1%9D" sido_name <- read_html(wiki_url) |> html_nodes("h3") |> html_text() |> str_remove("\\[편집\\]") minju_lst <- read_html(wiki_url) |> html_nodes(".wikitable") |> html_table() names(minju_lst) <- sido_name nomination <- sido_name |> enframe(value = "시도명", name="순번") |> mutate(data = minju_lst) |> unnest(data) |> select(-순번) |> janitor::clean_names(ascii = FALSE) nomination |> # write_rds("data/nomination.rds") write_csv("data/nomination.csv") ``` # 출판 ## 요약표 ```{r} #| eval: false library(tidyverse) library(gt) library(gtExtras) nomination <- read_rds("data/nomination.rds") nomi_table <- nomination |> count(공천_유형, name = "선거구수", sort = TRUE) |> # arrange(desc(선거구수)) |> mutate(비율 = 선거구수 / sum(선거구수)) |> janitor::adorn_totals(name = "합계") nomi_gt <- nomi_table |> gt() |> gt_theme_538() |> cols_align(align = "center") |> fmt_percent(columns = 비율, decimals = 1) |> tab_options( heading.title.font.size = px(18L), column_labels.font.size = px(14L), table.font.size = px(11L) ) |> tab_header( title = md("더불어민주당 **공천** 유형"), subtitle = md("제21대 국회의원 선거") ) |> tab_style( style = cell_fill(color = "gray90"), locations = cells_body( rows = 공천_유형 == "단수 공천" ) ) |> tab_style( style = cell_text(color = "red"), locations = cells_body( rows = 공천_유형 == "단수 공천", columns = 비율 ) ) nomi_gt nomi_gt |> gt::gtsave(filename = "data/nomi_gt.png") ``` ![민주당 공천 유형 통계표](data/nomi_gt.png) # 지도 시각화 ```{r} #| eval: false library(sf) library(tidyverse) extrafont::loadfonts() precinct_nomination_tbl <- readxl::read_excel("data/precinct_nomination.xlsx") precinct <- st_read("data/2020_21_elec_253_simple.json") |> st_set_crs(4326) precinct_nomination_sf <- left_join(precinct |> select(SGG_Code, geometry), precinct_nomination_tbl, by = "SGG_Code") st_geometry(precinct_nomination_sf) |> plot() nomination_type_gg <- ggplot() + geom_sf(data = precinct_nomination_sf, aes(geometry = geometry, fill = 공천_유형)) + theme_void(base_family = "AppleGothic") + theme(legend.position = "left") + scale_fill_manual(values = c("경선 공천" = "gray90", "단수 공천" = "red", "전략 공천" = "pink", "청년 경선 공천" = "black")) + labs(title = "대한민국 제21대 국회의원 선거 더불어민주당 후보", caption = "자료출처: 위키백과") metro_bb <- precinct_nomination_sf |> filter(SGG_1 == "경기") |> st_bbox() precinct_nomination_sf |> filter(SGG_1 == "경기") |> st_geometry() |> plot() metro_nomination_type_gg <- ggplot() + geom_sf(data = precinct_nomination_sf |> filter(SGG_1 %in% c("서울", "경기")), aes(geometry = geometry, fill = 공천_유형)) + theme_void(base_family = "AppleGothic") + coord_sf(xlim = c(metro_bb['xmin'], metro_bb['xmax']), ylim = c(metro_bb['ymin'], metro_bb['ymax']), expand = FALSE) + theme(legend.position = "none") + scale_fill_manual(values = c("경선 공천" = "gray90", "단수 공천" = "red", "전략 공천" = "pink", "청년 경선 공천" = "black")) + ggrepel::geom_text_repel( data = precinct_nomination_sf |> filter(SGG_1 %in% c("서울", "경기")) |> mutate(SGG_3 = glue::glue("{ifelse(str_detect(공천_유형,'단수'), SGG_3, '')}")), aes(label = SGG_3, geometry = geometry), stat = "sf_coordinates", min.segment.length = 1, size = 4, max.overlaps = Inf, family="AppleGothic" ) library(patchwork) nomination_type_gg / metro_nomination_type_gg nomination_type_final <- nomination_type_gg + metro_nomination_type_gg + plot_layout(widths = c(1, 2)) nomination_type_final ggplot2::ggsave("data/two_map.png") ragg::agg_jpeg("data/two_map.jpg", width = 10, height = 7, units = "in", res = 600) nomination_type_final dev.off() ``` ![](data/two_map.jpg) ``` # Self-contained TRUE format: html: self-contained: true # Hello World <!DOCTYPE html> <html> <head> <title>Hello, World!</title> </head> <body> <h1>Hello, World!</h1> <p>This is a basic HTML page.</p> </body> </html>