using the required libraries
library(readxl)
library(dplyr)
library(dygraphs)
reading the excel file
Assam <- read_excel("Assam.xlsx")
checking if the file is succesfully loaded
head(Assam)
## # A tibble: 6 x 4
## Year Month Rainfall Station
## <dbl> <chr> <dbl> <chr>
## 1 1901 Jan 27.1 Assam & Meghalaya
## 2 1902 Jan 9.3 Assam & Meghalaya
## 3 1903 Jan 19.9 Assam & Meghalaya
## 4 1904 Jan 11.1 Assam & Meghalaya
## 5 1905 Jan 19.9 Assam & Meghalaya
## 6 1906 Jan 9.7 Assam & Meghalaya
tail(Assam)
## # A tibble: 6 x 4
## Year Month Rainfall Station
## <dbl> <chr> <dbl> <chr>
## 1 2010 Dec 7.6 Assam & Meghalaya
## 2 2011 Dec 3.5 Assam & Meghalaya
## 3 2012 Dec 2.3 Assam & Meghalaya
## 4 2013 Dec 2 Assam & Meghalaya
## 5 2014 Dec 0.4 Assam & Meghalaya
## 6 2015 Dec 15.2 Assam & Meghalaya
Assam%>% group_by(Year,Month) %>%
summarise(Rainfall=sum(Rainfall)) %>% ungroup %>% transmute(Rainfall) %>%
ts(start=c(1901,1),freq=12) -> rain_ts
rain_ts %>% window(c(1901,1),c(1902,12))
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1901 223.0 464.1 1.2 19.5 27.1 430.6 524.9 30.6 207.0 115.6 163.7 291.4
## 1902 350.0 536.0 1.3 10.2 9.3 510.8 620.7 105.6 262.1 7.8 97.0 441.3
The above code would translate into: Take the rain data then group it by year and month then summarize the sum of rainfall, then ungroup (which undoes the group_by, and this is necessary for the transmute function ), then transmute the rainfall (which modifies values in the data frame and drops unreferenced variables), and create a monthly time series which has January 1901 as the starting date. A new data frame is created, names rain_ts, which will be used in subsequent code.
Then create a dygraph with an interactive window of the total rainfall for the period 1901-2016. The command dyRangeSelector creates a range selector at the bottom of the graph which allows you zoom in on any time frame of your choice within the period 1901-2016 to evaluate rainfall patterns and/or amounts.
rain_ts %>% dygraph(width=800,height=300,main="Assam And Meghalaya Rainfall 1901 to 2016") %>% dyRangeSelector
Adding an interactive rolling mean
The dyRoller control was then used to apply a rolling mean to each of these dygraphs. A rollPeriod of 240 months was selected, meaning that a 20 year moving mean was calculated. This allows the long-term trend in the monthly rainfall for the station(Assam and Meghalaya) to be more clearly analysed.The peroid of months can be changed according the need of the user
rain_ts %>% dygraph(width=800,height=400,main="Assam And Meghalaya Rainfall 1901 to 2016") %>% dyRoller(rollPeriod= 240)