Created
July 22, 2018 16:15
-
-
Save Gedevan-Aleksizde/14e02581f1b4ca228d4f3d9337ee6049 to your computer and use it in GitHub Desktop.
気象庁からダウンロードした気温データでヒートマップ作成する
This file contains 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
require(tidyverse) | |
require(ggthemes) | |
require(rjson) | |
require(jsonlite) | |
# 参考 | |
# https://www.data.jma.go.jp/gmd/risk/obsdl/index.php | |
# https://www.data.jma.go.jp/gmd/risk/obsdl/top/help3.html#hukajoho | |
# https://twitter.com/mehori/status/1020644999703089152 | |
# 気象庁からダウンロードしたファイル群を読み込む場合 | |
# https://www.data.jma.go.jp/gmd/risk/obsdl/index.php | |
# 日平均気温, 日最高気温, 日最低気温 を日別値で選択したことを前提 | |
tempera_kyoto <- list.files(path = "data/kyoto/", pattern="*.csv", full.names=T) %>% | |
map_df(~read_csv(., skip=6, locale=locale(encoding="cp932"), | |
col_names=c("date", | |
"avg_temp", "avg_temp_quality", "avg_temp_loc", | |
"max_temp", "max_temp_quality", "max_temp_loc", | |
"min_temp", "min_temp_quality", "min_temp_loc" | |
), | |
col_types = c("cdccdccdcc") | |
) | |
) %>% mutate(date=as.Date(date)) %>% | |
mutate(source_id=format(date, "%Y") %>% substr(start=1, stop=3)) %>% | |
mutate_if(is.character, factor) %>% | |
mutate(location="Kyoto") %>% | |
mutate(year=format(date, "%Y") %>%as.integer, | |
month_day=paste("2018", format(date, "%m-%d"), sep="-") %>% as.Date) | |
# https://toyokeizai.net/sp/visual/tko/temperature/ のデータを読み込む場合 | |
tempera_tokyo <- jsonlite::read_json("data/tokyo/temperature.json") %>% lapply(function(x) unlist(x)) | |
tempera_tokyo[[143]] <- c(tempera_tokyo[[143]], rep(NA, length(tempera_tokyo[[142]])-length(tempera_tokyo[[143]]))) | |
tempera_tokyo <- tempera_tokyo %>% as.data.frame | |
colnames(tempera_tokyo) <- 1876:2018 | |
tempera_tokyo$month_day <- seq(as.Date("2018-06-01"), as.Date("2018-09-30"), by="day") | |
tempera_tokyo <- tempera_tokyo %>% gather(key = "year", value="avg_temp", -month_day) %>% | |
mutate(avg_temp=as.numeric(avg_temp)) %>% | |
mutate(year=as.integer(year)) %>% | |
mutate(location="Tokyo") | |
plot_heat <- function(data){ | |
g <- ggplot( | |
data %>% filter(between(month_day, as.Date("2018-06-01"), as.Date("2018-09-30"))), | |
aes(x=month_day, y=year, fill=avg_temp, color="grey") | |
) + | |
geom_tile(color='grey', width=1, height=1) + | |
scale_fill_distiller(name="日毎平均気温 (°C)", palette = "Spectral") + | |
labs(x="月日", y="年", | |
caption="数値は気象庁より\n https://www.data.jma.go.jp/gmd/risk/obsdl/index.php") + | |
scale_y_reverse() + scale_x_date(date_labels="%m-%d") + scale_color_discrete(guide=F) + coord_equal() + | |
theme_tufte() + theme(axis.title.y=element_text(angle=0, vjust=.5)) | |
return(g) | |
} | |
g <- plot_heat(tempera_kyoto) | |
print(g + labs(title="京都市の日毎平均気温")) | |
g <- plot_heat(tempera_tokyo) | |
print(g + labs(title="東京都の日毎平均気温", | |
caption="数値はここから\nhttps://toyokeizai.net/sp/visual/tko/temperature/")) | |
g <- ggplot( | |
tempera_kyoto %>% filter(between(month_day, as.Date("2018-06-01"), as.Date("2018-09-30"))), | |
aes(x=date, y=avg_temp, ymin=min_temp, | |
ymax=max_temp, color=avg_temp) | |
) + geom_line() + | |
scale_color_distiller(name="", palette = "Spectral") + | |
labs(x="", y="日毎平均気温 (°C)", | |
caption="数値は気象庁より\n https://www.data.jma.go.jp/gmd/risk/obsdl/index.php") + | |
theme_pander() | |
print(g + labs(title="京都市の日毎平均気温") + coord_cartesian(ylim=c(10, 38))) | |
g <- ggplot( | |
tempera_tokyo %>% mutate(date=paste(as.character(year), format(month_day, "%m-%d"), sep="-") %>% as.Date()), | |
aes(x=date, y=avg_temp, color=avg_temp) | |
) + geom_line() + | |
scale_color_distiller(name="", palette = "Spectral") + | |
labs(x="", y="日毎平均気温 (°C)", | |
caption="数値は気象庁より\n https://www.data.jma.go.jp/gmd/risk/obsdl/index.php") + | |
theme_pander() | |
print(g + labs(title="東京都の日毎平均気温", | |
caption="数値はここから\nhttps://toyokeizai.net/sp/visual/tko/temperature/") + | |
coord_cartesian(ylim=c(10, 38))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment