Sometimes it’s useful to make a quick plot to look at the patterns of time series data. The dataset is obtained from the University Dayton’s Average Daily Temperature archive. The folder contains the daily temperature from 1995 to 04/2017 of 325 cities (325 files).
Let’s do some data wrangling and plot an horizon graphs in this tutorial
library(tidyverse)
library(data.table)
library(readr)
# import data
#setwd("25_flowingdata/")
listf <- list.files("allsites/")
res <- list()
for (i in 1:325) {
dt <-fread(paste0("allsites/",listf[i]),data.table = TRUE,fill = TRUE)
dt$City <- substr(listf[i],1,nchar(listf[i])-4)
res[[i]] <- dt
}
df <-rbindlist(res)
dt <- copy(df)
head(df)
## V1 V2 V3 V4 City
## 1: 1 1 1995 -99 ABTIRANA
## 2: 1 2 1995 -99 ABTIRANA
## 3: 1 3 1995 -99 ABTIRANA
## 4: 1 4 1995 -99 ABTIRANA
## 5: 1 5 1995 -99 ABTIRANA
## 6: 1 6 1995 -99 ABTIRANA
# Merge 3 columms to format date
df$time <- with(df, paste0(V3,"-", V2,"-", V1))
df$time <- lubridate::ydm(df$time)
df <- na.omit(df)
df <- select(df,City,time,V4)
names(df)[3] <- "temp"
df <- df[temp == -99,temp :=0]
#Calculate the monthly temperature by city
df$monyear <- format(df$time, "%Y-%m")
df<- df[,list(temp= mean(temp)),by = list(City,monyear)]
df1 <- df[,temp.pc:= (temp - shift(temp,12,fill=NA))/shift(temp,12,fill=NA),by = c("City")]
head(df)
## City monyear temp temp.pc
## 1: ABTIRANA 1995-01 0 NA
## 2: ABTIRANA 1995-02 0 NA
## 3: ABTIRANA 1995-03 0 NA
## 4: ABTIRANA 1995-04 0 NA
## 5: ABTIRANA 1995-05 0 NA
## 6: ABTIRANA 1995-06 0 NA
# Plotting data
library(htmltools)
library(DT)
library(d3horizonR)
fun_city<-function (city){
res<- filter(df,City==city)$temp
return(res)
}
fun_city1<-function (city){
res<- filter(df,City==city)$temp.pc
return(res)
}
dtmp <- df %>% select(City) %>% unique()
dtmp1 <- dtmp %>%
mutate(x = lapply(City, fun_city)) %>%
mutate(x = lapply(x, function(dat) {
d3horizon_chr(
list(dat),
options = d3horizonOptions(height=50),
width = 400
)})) %>%
mutate(x2 = lapply(City, fun_city1)) %>%
mutate(x2 = lapply(x2, function(dat) {
d3horizon_chr(
list(dat),
options = d3horizonOptions(height=50),
width = 400
)}))
fig<-
datatable(
dtmp1,
caption = 'average monthly temperature',
escape = FALSE,
colnames= c("City","average monthly temperature","12 month % temperature change"),
options = list(
columnDefs = list(list(width="400px", targets = 2:3)),
fnDrawCallback = htmlwidgets::JS(
'
function(){
HTMLWidgets.staticRender();
}
'
)
)
) %>%
tagList(htmlwidgets::getDependency("d3horizon", "d3horizonR")) %>%
browsable()
fig
Thank you and see you next time.
For more information about horizon graphs: - https://bl.ocks.org/timelyportfolio/1c938792957f70cf6069e48f33bdc1b5 - http://flowingdata.com/2015/07/02/changing-price-of-food-items-and-horizon-graphs/ - http://lenkiefer.com/2017/04/23/horizon