Skip to content

Instantly share code, notes, and snippets.

@igproj-fusion
Last active May 1, 2024 00:50
Show Gist options
  • Save igproj-fusion/db2ff5fb55398d925b49a5ecf3a4300b to your computer and use it in GitHub Desktop.
Save igproj-fusion/db2ff5fb55398d925b49a5ecf3a4300b to your computer and use it in GitHub Desktop.
pacman::p_load(
tidyverse,
rvest,
xlsx,
sf)
YEAR = 2009
if(YEAR < 2009 | YEAR > 2023){
stop("'YEAR' not appropriate!")
}
if(YEAR %in% 2021:2023) {
ColName1 <- rlang::sym("調査年月日")} else {
if(YEAR %in% 2013:2020) {
ColName1 <- rlang::sym("年月日")
}
}
URL_base0 = "https://www.env.go.jp/nature/dobutsu/bird_flu/migratory/ap_wr_transit"
URL_base <- paste0(URL_base0, substr(as.character(YEAR), 3, 4), "/")
URL_loclatlon = "https://raw.githubusercontent.com/igproj-fusion/BirdMigratory/main/LocLatLon.csv"
jp_pref_geo = "https://github.com/igproj-fusion/R-gis/raw/main/japan_ver2.geojson"
Border = data.frame(lon <- c(127, 130, 138, 138),
lat <- c( 40, 40, 44, 46))
#####################################################
JP_PREF <- read_sf(jp_pref_geo)
#####################################################
bird_df <- read_html(paste0(URL_base, "index.html"))|>
html_nodes("a") %>%
html_attr("href") |>
as_tibble() |>
filter(grepl(pattern = "sp_csv", x = value)) |>
mutate(fname = gsub("sp_csv/", "", value)) |>
mutate(link = paste0(URL_base, value)) |>
select(fname, link)
tmp_dir <- tempdir()
Birds <- NULL
for(i in 1:nrow(bird_df))
{
file <- file.path(tmp_dir, bird_df$fname[i])
download.file(bird_df$link[i], destfile = file, mode = "wb")
d <- read.xlsx(file, sheetIndex = 1)
if(YEAR < 2013){
dt <- d |>
filter(!is.na(年)) |>
mutate(date = ymd(paste0(年, "-", 月, "-", 日))) |>
} else {
dt <- d |>
filter(!is.na(!!ColName1)) |>
mutate(date = ymd(!!ColName1))
}
Birds <- dt |>
select(date, loc = 調査地名, type = 種名, number = 数) |>
mutate(number = as.integer(number)) |>
rbind(Birds)
}
BIRDS_LOC <- left_join(Birds, read.csv(URL_loclatlon), by = c("loc" = "loc")) |>
st_as_sf(coords = c('lon', 'lat'), crs = st_crs(JP_PREF))
#####################################################
# Example
#####################################################
TYPE <- "カルガモ"
ggplot() +
geom_sf(data = JP_PREF) +
geom_path(data = Border, aes(x = lon, y = lat)) +
geom_sf(data = BIRDS_LOC |>
filter(type == TYPE) |>
group_by(loc) |>
summarize(total = sum(number)),
aes(size = total),
alpha = 0.8, color = "cornflowerblue") +
ylim(c(27.5, 46)) +
labs(title = paste0(TYPE, "飛来地 ", YEAR - 1, "秋~", YEAR, "春")) +
theme_void() +
theme(plot.margin= unit(c(1, 1, 1, 1), "lines"),
plot.title = element_text(size = rel(1.4)),
legend.position = "bottom")
BIRDS_LOC |>
filter(type == TYPE) |>
mutate(year = year(date)) |>
mutate(month = month(date)) |>
group_by(year, month, area) |>
summarize(total = sum(number)) |>
mutate(date = ymd(paste0(year, "-", month, "-15"))) |>
ggplot(aes(x = date, y = total)) +
geom_line(aes(color = area),
linewidth = 1) +
geom_point(aes(shape = area, color = area), size = 3) +
theme_light() +
labs(title = paste0(TYPE, "飛来数 ", YEAR - 1, "秋~", YEAR, "春"),
x = "",
y = "No. of Observations") +
theme(plot.margin= unit(c(1, 1, 1, 1), "lines"),
plot.title = element_text(size = rel(1.5)),
axis.title = element_text(size = rel(1.3),
lineheight = 0.3),
legend.title = element_text(size = rel(0)),
legend.position = "bottom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment