library(tidyverse)library(rvest)extrafont::loadfonts()yoon_url<-'https://namu.wiki/w/%EC%9C%A4%EC%84%9D%EC%97%B4/%EC%A7%80%EC%A7%80%EC%9C%A8'yoon_html<-read_html(yoon_url)yoon_list<-yoon_html|>html_nodes('table')|>html_table(header =TRUE)yoon_raw<-yoon_list|>enframe()|>mutate(nrow =map_int(value, nrow))|>filter(nrow>54)|>mutate(조사회사 =c("한국갤럽", "리얼미터", "전국지표조사"))clean_tbl<-function(dataframe){three_tbl<-dataframe|>janitor::clean_names(ascii =FALSE)|>select(1:3)|>set_names(c("조사기간", "긍정", "부정"))|>filter(str_detect(조사기간, "^\\d{4}년"))}yoon_tbl<-yoon_raw|>mutate(data =map(value, clean_tbl))|>select(조사회사, data)|>unnest(data)|>mutate(긍정 =parse_number(긍정), 부정 =parse_number(부정))|>fill(긍정, .direction ="down")|>fill(부정, .direction ="down")|>pivot_longer(긍정:부정, names_to ="긍부정", values_to ="지지율")convert_korean_date<-function(korean_date){# Split the input into componentscomponents<-strsplit(korean_date, " ")[[1]]# Extract year, month, and weekyear<-as.numeric(gsub("년", "", components[1]))month<-as.numeric(gsub("월", "", components[2]))week<-as.numeric(gsub("주", "", components[3]))# Construct the date - the first day of the month and year, then add weeksdate<-make_date(year, month, 1)+weeks(week-1)return(date)}yoon_gg_tbl<-yoon_tbl|>mutate(조사기간 =map(조사기간, convert_korean_date))|>unnest(조사기간)|>mutate(조사회사 =factor(조사회사, levels =c("한국갤럽", "리얼미터", "전국지표조사")))|>filter(조사회사=="한국갤럽",조사기간>=as.Date("2022-05-07"))election_interval<--interval(ymd("2024-04-10"), ymd('2023-08-05'))election_period<-as.period(election_interval, unit ="month")remaining_ymd<-glue::glue("남은 기간:\n {month(election_period)}월 {day(election_period)}일")yoon_approval_gg<-yoon_gg_tbl|>ggplot(aes(x =조사기간, y =지지율, color=긍부정))+geom_point(data =yoon_gg_tbl|>filter(조사기간==max(조사기간)), aes(x =조사기간, y =지지율, color=긍부정), size =1.5)+geom_hline(yintercept =50, linetype =2, color ="gray50")+geom_vline(xintercept =as.Date("2024-04-10"), linetype =1, color ="gray50")+geom_line()+scale_color_manual(values =c("긍정"="blue", "부정"="red"))+labs(x ="", y ="지지율(%)", title ="윤석열 대통령 지지율 추세")+theme(legend.position ="top")+expand_limits(x =c(as.Date("2022-05-10"), as.Date("2027-05-09")))+scale_x_date(date_labels ="%Y", breaks ="1 year")+geom_text(aes(x=as.Date("2023-12-01"), y =60, label =remaining_ymd), color ="gray10", size =3, family ="MaruBrui")+geom_text(aes(x=as.Date("2024-08-01"), y =65, label ="제22대 총선\n4월10일"), color ="gray10", size =3, family ="MaruBrui")yoon_approval_ggragg::agg_jpeg("images/대통령지지율/윤석열.jpeg", width =10, height =7, units ="in", res =600)yoon_approval_ggdev.off()
코드
library(tidyverse)library(rvest)moon_url<-'https://namu.wiki/w/%EB%AC%B8%EC%9E%AC%EC%9D%B8/%EC%A7%80%EC%A7%80%EC%9C%A8'moon_html<-read_html(moon_url)moon_list<-moon_html|>html_nodes('table')|>html_table(header =TRUE)moon_raw<-moon_list|>enframe()|>mutate(nrow =map_int(value, nrow))|>filter(nrow>30)|>mutate(id =row_number(), 조사회사 =ifelse(id>6, "리얼미터", "한국갤럽"))clean_tbl<-function(dataframe){three_tbl<-dataframe|>janitor::clean_names(ascii =FALSE)|>select(1:3)|>set_names(c("조사기간", "긍정", "부정"))|>filter(str_detect(조사기간, "^\\d{4}년"))}moon_tbl<-moon_raw|>mutate(data =map(value, clean_tbl))|>filter(nrow>250)|>select(조사회사, data)|>unnest(data)|>mutate(긍정 =parse_number(긍정), 부정 =parse_number(부정))|>fill(긍정, .direction ="down")|>fill(부정, .direction ="down")|>pivot_longer(긍정:부정, names_to ="긍부정", values_to ="지지율")convert_korean_date<-function(korean_date){# Split the input into componentscomponents<-strsplit(korean_date, " ")[[1]]# Extract year, month, and weekyear<-as.numeric(gsub("년", "", components[1]))month<-as.numeric(gsub("월", "", components[2]))week<-as.numeric(gsub("주", "", components[3]))# Construct the date - the first day of the month and year, then add weeksdate<-make_date(year, month, 1)+weeks(week-1)return(date)}moon_approval_gg<-moon_tbl|>mutate(조사기간 =map(조사기간, convert_korean_date))|>unnest(조사기간)|>mutate(조사회사 =factor(조사회사, levels =c("한국갤럽", "리얼미터")))|>filter(조사회사=="한국갤럽")|>ggplot(aes(x =조사기간, y =지지율, color=긍부정))+geom_hline(yintercept =50, linetype =2, color ="gray50")+geom_line()+# facet_wrap(~조사회사) +scale_color_manual(values =c("긍정"="blue", "부정"="red"))+labs(x ="", y ="지지율(%)", title ="문재인 대통령 지지율 추세")+theme(legend.position ="top")moon_approval_ggragg::agg_jpeg("images/대통령지지율/문재인.jpeg", width =10, height =7, units ="in", res =600)moon_approval_ggdev.off()
코드
park_url<-'https://namu.wiki/w/%EB%B0%95%EA%B7%BC%ED%98%9C/%EC%A7%80%EC%A7%80%EC%9C%A8'park_html<-read_html(park_url)park_list<-park_html|>html_nodes('table')|>html_table(header =TRUE)park_raw<-park_list|>enframe()|>mutate(nrow =map_int(value, nrow))|>filter(nrow>30)|>mutate(조사회사 ="한국갤럽")park_tbl<-park_raw|>mutate(data =map(value, clean_tbl))|>filter(nrow>200)|>select(조사회사, data)|>unnest(data)|>mutate(긍정 =parse_number(긍정), 부정 =parse_number(부정))|>fill(긍정, .direction ="down")|>fill(부정, .direction ="down")|>pivot_longer(긍정:부정, names_to ="긍부정", values_to ="지지율")park_approval_gg<-park_tbl|>mutate(조사기간 =map(조사기간, convert_korean_date))|>unnest(조사기간)|>ggplot(aes(x =조사기간, y =지지율, color=긍부정))+geom_hline(yintercept =50, linetype =2, color ="gray50")+geom_line()+scale_color_manual(values =c("긍정"="blue", "부정"="red"))+labs(x ="", y ="지지율(%)", title ="박근혜 대통령 지지율 추세")+theme(legend.position ="top")park_approval_ggragg::agg_jpeg("images/대통령지지율/박근혜.jpeg", width =10, height =7, units ="in", res =600)park_approval_ggdev.off()
코드
lee_url<-'https://namu.wiki/w/%EC%9D%B4%EB%AA%85%EB%B0%95/%EC%A7%80%EC%A7%80%EC%9C%A8'lee_html<-read_html(lee_url)lee_list<-lee_html|>html_nodes('table')|>html_table(header =TRUE)lee_raw<-lee_list|>enframe()|>mutate(nrow =map_int(value, nrow))|>filter(nrow>30)|>mutate(조사회사 ="한국갤럽")convert_ym_date<-function(korean_date){# Split the input into componentscomponents<-strsplit(korean_date, " ")[[1]]# Extract year and monthyear<-as.numeric(gsub("년", "", components[1]))month<-as.numeric(gsub("월", "", components[2]))# Construct the date - the first day of the month and yeardate<-make_date(year, month, 15)return(date)}lee_tbl<-lee_raw|>mutate(data =map(value, clean_tbl))|>filter(nrow>90)|>select(조사회사, data)|>unnest(data)|>mutate(긍정 =parse_number(긍정), 부정 =parse_number(부정))|>fill(긍정, .direction ="down")|>fill(부정, .direction ="down")|>separate(조사기간, into =c("시작", "끝"), sep ="-")|>select(-끝)|>mutate(조사기간 =case_when(str_detect(시작, "주$")~map(시작, convert_korean_date), TRUE~map(시작, convert_ym_date)))|>unnest(조사기간)|>mutate(조사기간 =as.Date(조사기간, origin ="1970-01-01"))previous_date<-ymd("2012-04-11")-election_periodlee_current_tbl<-lee_tbl|>pivot_longer(긍정:부정, names_to ="긍부정", values_to ="지지율")|>filter(조사기간==previous_date+9)lee_approval_gg<-lee_tbl|>pivot_longer(긍정:부정, names_to ="긍부정", values_to ="지지율")|>ggplot(aes(x =조사기간, y =지지율, color=긍부정))+geom_point(data =lee_current_tbl, aes(x =조사기간, y =지지율, color=긍부정))+geom_text(data =lee_current_tbl, aes(x =조사기간, y =지지율, color=긍부정, label =지지율), vjust =-1.5)+geom_hline(yintercept =50, linetype =2, color ="gray50")+geom_line()+scale_color_manual(values =c("긍정"="blue", "부정"="red"))+labs(x ="", y ="지지율(%)", title ="이명박 대통령 지지율 추세")+theme(legend.position ="top")+scale_x_date(date_labels ="%Y", breaks ="1 year")+geom_vline(xintercept =as.Date("2012-04-11"), linetype =1, color ="gray50")+geom_text(aes(x=as.Date("2011-12-01"), y =45, label =remaining_ymd), color ="gray10", size =3, family ="MaruBrui")+geom_text(aes(x=as.Date("2012-08-01"), y =65, label ="제19대 총선\n4월11일"), color ="gray10", size =3, family ="MaruBrui")lee_approval_ggragg::agg_jpeg("images/대통령지지율/이명박.jpeg", width =10, height =7, units ="in", res =600)lee_approval_ggdev.off()
코드
rho_url<-'https://namu.wiki/w/%ED%8B%80:%ED%95%9C%EA%B5%AD%EA%B0%A4%EB%9F%BD%20%EB%85%B8%EB%AC%B4%ED%98%84%20%EB%8C%80%ED%86%B5%EB%A0%B9%20%EA%B5%AD%EC%A0%95%EC%A7%80%EC%A7%80%EC%9C%A8'rho_html<-read_html(rho_url)rho_list<-rho_html|>html_nodes('table')|>html_table(header =TRUE)rho_raw<-rho_list|>enframe()|>mutate(nrow =map_int(value, nrow))|>filter(nrow>30)|>mutate(조사회사 ="한국갤럽")rho_tbl<-rho_raw|>mutate(data =map(value, clean_tbl))|>filter(nrow>20)|>select(조사회사, data)|>unnest(data)|>mutate(긍정 =parse_number(긍정), 부정 =parse_number(부정))|>fill(긍정, .direction ="down")|>fill(부정, .direction ="down")|>separate(조사기간, into =c("시작", "끝"), sep ="-")|>select(-끝)|>mutate(조사기간 =case_when(str_detect(시작, "주$")~map(시작, convert_korean_date), TRUE~map(시작, convert_ym_date)))|>unnest(조사기간)|>mutate(조사기간 =as.Date(조사기간, origin ="1970-01-01"))rho_approval_gg<-rho_tbl|>pivot_longer(긍정:부정, names_to ="긍부정", values_to ="지지율")|>ggplot(aes(x =조사기간, y =지지율, color=긍부정))+geom_hline(yintercept =50, linetype =2, color ="gray50")+geom_line()+scale_color_manual(values =c("긍정"="blue", "부정"="red"))+labs(x ="", y ="지지율(%)", title ="노무현 대통령 지지율 추세")+theme(legend.position ="top")rho_approval_ggragg::agg_jpeg("images/대통령지지율/노무현.jpeg", width =10, height =7, units ="in", res =600)rho_approval_ggdev.off()
library(patchwork)all_approval_gg<-(rho_approval_gg+lee_approval_gg+park_approval_gg)/(moon_approval_gg+yoon_approval_gg)ragg::agg_jpeg("images/대통령지지율/전체대통령.jpeg", width =10, height =7, units ="in", res =600)all_approval_ggdev.off()
코드
two_approval_gg<-(lee_approval_gg)/(yoon_approval_gg)ragg::agg_jpeg("images/대통령지지율/두대통령.jpeg", width =10, height =7, units ="in", res =600)two_approval_ggdev.off()