먼저 작업에 필요한 패키지들을 불러오도록 하자.
## Loading required package: xml2
## ── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1 ✓ purrr 0.3.3
## ✓ tibble 2.1.3 ✓ dplyr 0.8.3
## ✓ tidyr 1.0.0 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.4.0
## ── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x readr::guess_encoding() masks rvest::guess_encoding()
## x dplyr::lag() masks stats::lag()
## x purrr::pluck() masks rvest::pluck()
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
함수를 사용하여 홈페이지로부터 자료를 추출한다. 각 함수의 의미는 다음과 같다.
git_info <- git_url %>% # git_url에 할당된 주소에 있는 노드를 html로 읽어들인 후 git_info 라는 객체에 재할당
read_html() %>%
html_nodes(".border.border-gray-dark.py-2.graph-before-activity-overview") %>%
html_nodes(".js-calendar-graph.mx-3.d-flex.flex-column.flex-items-end.flex-xl-items-center.overflow-hidden.pt-1.is-graph-loading.graph-canvas.calendar-graph.height-full.text-center") %>%
html_nodes("g") %>%
html_nodes("rect")
commit_date <- git_info %>% # git_info 에서 읽어들인 노드 위에 날짜를 표시하는 태그를 가져오고 이를 날짜형으로 변형
html_attr("data-date") %>%
as.Date()
commit_count <- git_info %>% # git_info 에서 읽어들인 노드 위에 날짜 수를 세는 태그를 가져오고 이를 숫자형으로 변형
html_attr("data-count") %>%
as.numeric()
fill <- git_info %>% # 색상 코드를 추출
html_attr("fill")
commit_info <- data.frame(commit_date,commit_count,fill) %>% # 그래프를 그리기 위해 객체를 묶고, 커밋 수 별로 값을 지정
mutate(level = case_when(
fill == unique(fill)[1] ~ "E",
fill == unique(fill)[2] ~ "D",
fill == unique(fill)[3] ~ "C",
fill == unique(fill)[4] ~ "B",
TRUE ~ "A"
))
####
uniform_date <- 100 # 100일 간의 데이터로 제한전처리한 데이터를 가지고 ggplot을 이용해 반응형 그래프를 생성해보았다.
# ggplot
git_vis <- ggplot(data = tail(commit_info,uniform_date), aes(x=commit_date,y=commit_count,fill=level)) +
ggtitle(paste0("최근 커밋수: ", uniform_date)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = levels(commit_info$fill)) +
theme_bw() +
labs(x="Date", y="Commit count")
ggplotly(git_vis)