# 데이터 로드
useable_dfs <- data.frame()
for (filepath in dir(path = "~/R/scrapped/", pattern = "[.]xlsx$")) {
scrapped <- read_excel(filepath)
useable_dfs <- bind_rows(useable_dfs, scrapped)
}
# 단순중복 체크
useable_dfs[duplicated(useable_dfs),] %>% nrow()
## [1] 215712
# 단순중복(동일한 데이터) 제거
useable_dfs <- useable_dfs[!duplicated(useable_dfs),]
# 작업용 Raw 데이터 사본 생성
parking <- useable_dfs
# 분단위 데이터 한정(동일 분 내의 다른 데이터는 제거)
substr(parking$datetime[1], 1, 16) # 초 이하 버림
## [1] "2019-08-04 16:55"
# 임시 컬럼 dup 생성
parking$dup <- paste(parking$`주차장명`, as.POSIXct(substr(parking$datetime, 1, 16)))
x <- parking[duplicated(parking$dup),]
# 중복 제거
parking <- parking[!duplicated(parking$dup),] %>% select(-dup)
# 자료형 변환
parking$datetime <- as.POSIXct(parking$datetime) # POSIX로 변환
parking$`주차면수` <- as.numeric(as.character(parking$`주차면수`))
parking$`잔여주차가능대수` <- as.numeric(as.character(parking$`잔여주차가능대수`))
#parking$번호 <- as.factor(parking$번호)
# 시간 추가
parking$hour <- lubridate::hour(parking$datetime)
Chart <- parking %>% group_by(hour) %>% tally(mean(`잔여주차가능대수`))
ggplot(Chart, aes(x = hour, y = n)) +
geom_area()
ggsave("1_area.png", width = 6, height = 4, units = "in", dpi = 200)
ggplot(Chart, aes(x = hour, y = n)) +
geom_line()
# 사용자정의 함수 이용(반복 사용되는 인수를 간편하게 수정하기 위함)
f.ggsave("2_line.png")
# 주차장별로 구분
Chart <- parking %>% group_by(`주차장명`, hour) %>% tally(mean(`잔여주차가능대수`))
ggplot(Chart, aes(x = hour, y = n)) +
geom_line() +
facet_wrap(vars(`주차장명`))
ggsave("3_주차장구분.png", width = 12, height = 8, units = "in", dpi = 100)
# 가로세로 적당한 크기로 펼쳐줍니다.
ggplot(Chart, aes(x = hour, y = n)) +
geom_line() +
facet_wrap(`주차장명` ~ ., ncol = 4)
f.ggsave(filename = "4_주차장facet(4x8).png", target = "facet")
## 스케일이 달라 거의 직선으로 보이는 경우(서울역관광버스주차장)
## 가용 비율로 변경
### -> 더 간단한 해결방법. 단, 플롯마다 y축 텍스트가 추가되어 복잡
ggplot(Chart, aes(x = hour, y = n)) +
geom_line() +
facet_wrap(`주차장명` ~ ., ncol = 4, scales = "free")
f.ggsave("4-1_주차장facet(4x8).png", "facet")
###
# 주차면수 불러오기
x <- parking %>% distinct(`주차장명`, `주차면수`)
# 주차면수 join
Chart <- left_join(Chart, x[1:32,])
## Joining, by = "주차장명"
# 여유 비율을 ratio로 추가
Chart %>% mutate(ratio = n/`주차면수`*100) %>%
ggplot(aes(x = hour, y = ratio)) +
geom_line() +
facet_wrap(`주차장명` ~ ., ncol=4)
f.ggsave("5_주차가용비율.png", "facet")
Chart %>% mutate(ratio = n/`주차면수`*100) %>%
ggplot(aes(x = hour, y = ratio)) +
geom_line() +
scale_x_continuous(breaks = seq(0, 23, 3)) +
facet_wrap(`주차장명` ~ ., ncol=4) +
theme_minimal()
f.ggsave("5-1_테마변경.png", "facet")
# Plot <- Chart %>% mutate(ratio = n/주차면수*100) %>%
# ggplot(aes(x = hour, y = ratio)) +
# geom_line() +
# scale_x_continuous(breaks = seq(0, 23, 3)) +
# facet_wrap(주차장명 ~ ., ncol=4) +
# theme_minimal()
# Plot +
# annotate("text", lubridate::hour(Sys.time()), y = Chart$ratio, label = paste0(Chart$ratio,"%"))
# 현재시간대와 동일 시간대의 데이터만 확인
Chart %>% filter(hour == lubridate::hour(Sys.time()))
## # A tibble: 32 x 4
## # Groups: 주차장명 [32]
## 주차장명 hour n 주차면수
## <chr> <int> <dbl> <dbl>
## 1 가양라이품 18 26.2 38
## 2 개화산역 18 165. 322
## 3 개화역 18 158. 483
## 4 구로디지털단지역 18 0.919 92
## 5 구파발역 18 180. 399
## 6 동대문 18 979. 1092
## 7 마포유수지 18 393. 505
## 8 복정역 18 180. 363
## 9 사당노외 18 35.4 184
## 10 서울역관광버스주차장 18 35 33
## # ... with 22 more rows
#####################
# Chart <- Chart %>% mutate(ratio = n/주차면수*100)
Chart$ratio <- Chart$n/Chart$`주차면수`*100
Chart$label <- ifelse(Chart$hour == lubridate::hour(Sys.time()), paste0(round(Chart$ratio, digits = 2),"%"), NA)
Plot <- ggplot(Chart, aes(x = hour, y = ratio)) +
geom_line() +
geom_text(aes(label = label,
x = lubridate::hour(Sys.time()), y = ratio)) +
# annotate("text", lubridate::hour(Sys.time()), y = 100, alpha=0.3, label = paste0("?","%")) +
geom_vline(xintercept = lubridate::hour(Sys.time()), color = "red", linetype = 2) + # as.numeric() transformation
scale_x_continuous(breaks = seq(0, 23, 3)) +
theme(panel.grid.major = element_line(color = "blue", size = 1)) +
facet_wrap(`주차장명` ~ ., ncol=4) +
theme_minimal()
ggsave("5-2_수직선라벨추가.png", Plot, width = 12, height = 8, units = "in", dpi = 100)
## Warning: Removed 736 rows containing missing values (geom_text).
# current_hour <- 12 # 하드코딩
current_hour <- lubridate::hour(Sys.time()) # 현재시간대 이용
Chart$label <- ifelse(Chart$hour == current_hour, paste0(round(Chart$ratio, digits = 2),"%"), NA)
Plot <- ggplot(Chart, aes(x = hour, y = ratio)) +
geom_line() +
geom_text(aes(label = label, x = current_hour, y = ratio,
vjust = 0, hjust = 0), size = 3) +
geom_vline(xintercept = current_hour, color = "red", linetype = 2) + # as.numeric() transformation
scale_x_continuous(breaks = seq(0, 23, 3)) +
theme(panel.grid.major = element_line(color = "blue", size = 1)) +
facet_wrap(`주차장명` ~ ., ncol=4) +
theme_minimal()
ggsave("6_튜닝(현재시각기준).png", Plot, width = 12, height = 8, units = "in", dpi = 100)
## Warning: Removed 736 rows containing missing values (geom_text).
current_hour <- 12 # 다른 시간대 확인을 위해 하드코딩
Chart$label <- ifelse(Chart$hour == current_hour, paste0(round(Chart$ratio, digits = 2),"%"), NA)
Plot <- ggplot(Chart, aes(x = hour, y = ratio)) +
geom_line() +
geom_text(aes(label = label, x = current_hour, y = ratio,
vjust = 0, hjust = 0), size = 3) +
geom_vline(xintercept = current_hour, color = "red", linetype = 2) + # as.numeric() transformation
scale_x_continuous(breaks = seq(0, 23, 3)) +
theme(panel.grid.major = element_line(color = "blue", size = 1)) +
facet_wrap(`주차장명` ~ ., ncol=4) +
theme_minimal()
ggsave("6-1_시간변경.png", Plot, width = 12, height = 8, units = "in", dpi = 100)
## Warning: Removed 736 rows containing missing values (geom_text).
# for 루프로 전체 시간대 확인
for (current_hour in seq(0,23)) {
Chart$label <- ifelse(Chart$hour == current_hour, paste0(round(Chart$ratio, digits = 2),"%"), NA)
Plot <- ggplot(Chart, aes(x = hour, y = ratio)) +
geom_line() +
geom_text(aes(label = label, x = current_hour, y = ratio,
vjust = 0, hjust = 0), size = 3) +
geom_vline(xintercept = current_hour, color = "red", linetype = 2) +
scale_x_continuous(breaks = seq(0, 23, 3)) +
theme(panel.grid.major = element_line(color = "blue", size = 1)) +
facet_wrap(`주차장명` ~ ., ncol=4) +
theme_minimal()
ggsave(sprintf("7_%02d시.png", current_hour), Plot, width = 12, height = 8, units = "in", dpi = 100)
}
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
## Warning: Removed 736 rows containing missing values (geom_text).
This R Markdown document is made interactive using Shiny. Unlike the more traditional workflow of creating static reports, you can now create documents that allow your readers to change the assumptions underlying your analysis and see the results immediately.
To learn more, see Interactive Documents.
Note the use of the height parameter to determine how much vertical space the embedded application should occupy.
You can also use the shinyApp function to define an application inline rather then in an external directory.
In all of R code chunks above the echo = FALSE attribute is used. This is to prevent the R code within the chunk from rendering in the document alongside the Shiny components.