Preparation

The aim of this exercise is to create a dynamic graph, or dygraph, which presents a time series of monthly rainfall for four Irish weather stations. First, the following libraries must be loaded in R;

library(dplyr)
library(dygraphs)

The dplyr library provides a more flexible grammar for data manipulation. The R code for this exercise makes use of this flexible grammar. Meanwhile, the dygraphs library allows for the creation of dynamic graphs.

The main dataset used in this exercise is the rainfall.RData file. This file contains monthly rainfall data for 25 Irish weather stations from 1850 to 2014, with no missing values. The dataset is supplied by Dr Conor Murphy and Dr Simon Noone of Maynooth University, and has been converted to an R binary file for convenience. Once the file has been downloaded and its folder location set as the working directory, it can be loaded into the global environment;

load('rainfall.RData')

As stated above, this exercise will use four of the stations contained within the dataset:

First, a time series for each station must be created

#Dublin
rain %>%  group_by(Year,Month) %>% filter(Station=="Dublin Airport") %>%
  summarise(Rainfall=sum(Rainfall)) %>% ungroup %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  Dublin

#Belfast
rain %>%  group_by(Year,Month) %>% filter(Station=="Belfast") %>%
  summarise(Rainfall=sum(Rainfall)) %>% ungroup %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  Belfast

#Galway
rain %>%  group_by(Year,Month) %>% filter(Station=="University College Galway") %>%
  summarise(Rainfall=sum(Rainfall)) %>% ungroup %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  Galway

#Cork
rain %>%  group_by(Year,Month) %>% filter(Station=="Cork Airport") %>%
  summarise(Rainfall=sum(Rainfall)) %>% ungroup %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  Cork

The complete time series is generated by grouping the rainfall totals according to year and month. The individual stations are then extracted using the filter function. Finally, cbind combines the four individual time series into one.

all_ts <- cbind(Belfast,Dublin,Galway,Cork)
window(all_ts,c(1850,1),c(1850,5))
##          Belfast Dublin Galway  Cork
## Jan 1850   115.7   75.8  108.9 155.3
## Feb 1850   120.5   47.8  131.5  92.6
## Mar 1850    56.8   18.5   56.6  56.0
## Apr 1850   142.6   97.5  120.5 207.2
## May 1850    57.9   58.6   69.8  35.3

Creating the Graph

A dynamic graph can now be created for the all_ts time series;

all_ts %>% dygraph(main="Rainfall Time Series (mm)",width=800,height=360) %>% dyRangeSelector %>% dyRoller %>% 
    dySeries("Belfast", label="Belfast", color="orange") %>%
    dySeries("Cork", label="Cork Airport", color="red") %>%
    dySeries("Galway", label="University College Galway", color="green") %>%
    dySeries("Dublin", label="Dublin Airport", color="navy") %>%
  dyLegend(width=500)

The dyRangeSelector extension inserts an interactive window, useful for exploring specific areas of the time series. Using dyRoller inserts an interactive rolling mean, which helps to smooth out short-term fluctuations and highlight more long-term trends. Colours and labels are specified using the dySeries extension. Inputting the legend width using dyLegend stops it from wrapping over to a second line.

Alternatively, the time series can be displayed using four separate dynamic graphs;

Dublin %>% dygraph(width=800,height=130,group="dub_bel_gal_cor",main="Dublin") %>% dyRoller
Belfast %>% dygraph(width=800,height=130,group="dub_bel_gal_cor",main="Belfast") %>% dyRoller
Galway %>% dygraph(width=800,height=130,group="dub_bel_gal_cor",main="Galway") %>% dyRoller
Cork %>% dygraph(width=800,height=170,group="dub_bel_gal_cor",main="Cork") %>% dyRoller %>% dyRangeSelector

Interpreting the data

The station at Dublin Airport received the least amount of rainfall over the course of the time series; its wettest month was December 1978, with a total of 217 millimetres. December 1978 was also the wettest month in Belfast, which received 329.5 mm. Smoothing out the data (setting the rolling mean to 100) reveals a slight increasing trend over time in Dublin, but not too much fluctuation. Belfast is wetter on average, with a clear increasing trend since the year 2000.

Galway and Cork are the two wettest stations over the course of the time series. The wettest month recorded at University College Galway was November 2009, with a rainfall total of 329.5 mm. Data smoothing (rolling mean at 100) reveals that Galway’s average rainfall has increased over time, particularly from 2000 onwards. Cork Airport received the most rainfall over time. The highest monthly total recorded at the station was 460.5 mm in December 1899.

The increasing trend observed at the other stations is not present at Cork Airport. Smoothing out the data reveals a period of high average rainfall which took place in the 1870s and 1880s. Average rainfall then started to decrease, before levelling out around the mid-20th century. In fact, data smoothing shows Galway beginning to overtake Cork, with higher average rainfall in the latter part of the record.

References

The R Foundation for Statistical Computing (2019) RStudio (1.1.463) [computer program] Available at: https://www.rstudio.com/ (accessed 18 Jan 2019).