Created
October 8, 2023 09:45
-
-
Save tetlabo/aa0c47621910fd7cfc60dc20e7a0259e to your computer and use it in GitHub Desktop.
専修大学図書館生田本館の混雑率データを取得するRプログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(lubridate) | |
library(scales) | |
library(ggthemes) | |
options(dplyr.width = Inf, scipen = 1, digits = 4) | |
# 任意のフォントに変更してください | |
theme_set(theme_fivethirtyeight(base_family = "IPAexGothic", base_size = 12)) | |
theme_update(plot.title = element_text(size = 14), axis.title = element_text()) | |
library(rvest) | |
library(tibbletime) | |
# 2023-04-01以前はデータが記録されていないようです | |
start_date = ymd("2023-04-01", tz = "Asia/Tokyo") | |
# 任意の終了日を指定してください | |
end_date <- ymd("2023-10-05", tz = "Asia/Tokyo") | |
date_range <- seq.POSIXt(start_date, end_date, by = "1 day") | |
url_base = "https://opac.lib.senshu-u.ac.jp/iwjs0008opc/graph/honkan/graph_" | |
df <- data.frame() | |
for (day in date_range){ | |
date <- format.Date(day, "%Y%m%d") | |
url <- paste0(url_base, date, ".html") | |
html <- read_html(url) | |
datas <- html %>% html_element(".graph") %>% html_text() | |
data <- datas %>% str_match(., "data: \\[.+?\\]") %>% str_replace(., "data: \\[", "") %>% str_replace(., "\\]$", "") %>% strsplit(., ",") %>% unlist() %>% as.numeric() | |
times <- seq(ymd_hm(paste(date, "09:30"), tz = "Asia/Tokyo"), ymd_hm(paste(date, "20:30"), tz = "Asia/Tokyo"), by = "hour") | |
df_tmp <- data.frame(time = times, data = data) | |
df <- rbind.data.frame(df, df_tmp) | |
} | |
roll_ave <- rollify(mean, window = 12, na_value = NA) | |
df <- df %>% mutate(ave = roll_ave(data)) | |
write_excel_csv(df, "library_congestion_data.csv") | |
ggplot(df, aes(x = time, y = data)) + | |
geom_line(aes(colour = "毎時データ"), linewidth = 1.1) + | |
geom_line(aes(y = ave, colour = "1日平均データ"), linewidth = 1.1, ) + | |
scale_x_datetime(date_breaks = "1 month", date_label = "%m/%d") + | |
labs(title = "専修大学図書館生田本館の混雑率の推移", x = "", y = "混雑度 (%)") + | |
scale_colour_manual(name = "混雑度", breaks = c("毎時データ", "1日平均データ"), values = c("毎時データ" = "gray80", "1日平均データ" = "#008fd5")) + | |
theme(legend.position = "top") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment