関東甲信・梅雨入り日の推移

気象庁のウェブサイトに「昭和26年(1951年)以降の梅雨入りと梅雨明け(確定値):関東甲信」のページがある。

ここに掲載されている表(table)を例に、ウェブスクレイピングを行ってみた(それに続く処理は参考である)。

library(rvest)
library(dplyr)


dt <- read_html("https://www.data.jma.go.jp/fcd/yoho/baiu/kako_baiu09.html")

dt %>% html_nodes(xpath = "//table") %>% html_text() -> tab
tab2 <- unlist(strsplit(tab [ [ 2 ] ], "\n"))

> head(tab2)
[1] "関東甲信"
[2] "年"
[3] "入り"
[4] "明け"
[5] "梅雨の時期の降水量の平年比(地域平均値)(%)"
[6] "1951年6月15日ごろ7月18日ごろ121 "
> tail(tab2)
[1] "2014年6月 5日ごろ7月21日ごろ116 "
[2] "2015年6月 3日ごろ7月10日ごろ128 "
[3] "2016年6月 5日ごろ7月29日ごろ74 "
[4] "2017年6月 7日ごろ7月 6日ごろ71 "
[5] "2018年6月 6日ごろ6月29日ごろ92 "
[6] "平 年6月 8日ごろ7月21日ごろ  "

 このように、ヘッダー部分が5個、その後に各年のデータ、そして最後に平年データという具合に、表の要素が文字列として取得できる。

 

以下は、これらの文字列から梅雨入り日を抽出し、回帰直線とともにプロットするスクリプト

library(ggplot2)
library(ggthemes)


date <- chartr("月", "-", substr(tab2[6:(length(tab2) - 1)], 6, 9))
Date <- as.Date(paste0("2000-", date))
year <- 1951:(1950 + length(Date))
df <- data.frame(year, Date)

 

g <- ggplot(data = df, aes(x = year, y = Date))
g <- g + geom_line(color = "darkblue", size = 0.5, alpha = 0.4)
g <- g + geom_point(color = "darkblue")
g <- g + stat_smooth(method = "lm", se = F, color = "grey60")
g <- g + theme_economist()
g <- g + theme(axis.title = element_text(size = 15))
print(g)

f:id:fusion0202:20190530130033p:plain