INTRODUCTION

This blog gives the procedures for making two dygraphs of four weather stations- Belfast, Dublin Airport, University College Galway and Cork Airport depicting time series of rainfall data on monthly basis from the year 1850 up to 2015.

Step 1: Load R packages that will enable the running of these codes

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(dygraphs)
library(sp)
library(RColorBrewer)
load("Rainfall.Rdata")

Step 2 : Import rainfall data into the working database and define a working directory.

It consist of 25 rainfall stations in Ireland whereby the data file only has the rainfall data to every month. This data file has four headings namely; Year, Month, Rainfall and Stations. The time series is from 1850 to 2014.

Step 3 : Generate many dygraphs with monthly time series of 1850.

The first of these is to use the command group_by () to group the data by year and month, then filters the data by station, and finally, calculate the total rainfall for each group using the summarise() and sum() commands. Doing this after ungrouping, the code remains with the Rainfall column using transmute(). Lastly, transform the data to a monthly time series using ‘ts()’ and specify that the frequency of the data is 12 months.

Four time series objects are produced for four stations (Dublin Airport, Belfast, University College Galway & Cork Airport), and joined using the ‘cbind()’ function. Lastly, the ‘window()’ function brings the data for the year 1850 as you can override the grouping behaviour with ‘groups’.

rain %>%
  group_by(Year, Month) %>%
  summarise(Rainfall = sum(Rainfall), .groups = "drop") %>%
  ungroup() %>%
  transmute(Rainfall) %>%
  ts(start = c(1850, 1), freq = 12) -> rain_ts
rain_ts %>% window(c(1870,1),c(1877,12))
##         Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct
## 1870 2666.2 1975.3 1500.5 1024.8 1862.8  789.2 1038.6 1510.5 2045.5 5177.6
## 1871 3148.3 2343.7 1731.7 2654.5  657.6 2040.1 3705.0 1869.9 2083.4 2774.3
## 1872 3822.1 2940.2 2237.3 1732.2 1418.5 2763.0 2204.2 2892.3 3114.7 3499.0
## 1873 3999.5 1097.5 2292.9  970.9 1440.6 1249.0 3350.5 3793.3 2361.3 2817.0
## 1874 2029.5 2016.1 1351.3 1549.9  972.9 1075.5 2049.7 3011.9 2690.0 3447.7
## 1875 4165.4 1327.0 1016.0  884.2 1698.4 2371.0 1976.2 2038.9 3727.2 4327.8
## 1876 1401.1 3360.9 2780.2 2034.0  501.7 1296.2 1298.0 2583.2 3142.8 3369.5
## 1877 4273.1 1855.2 2154.0 2956.1 1908.2 2084.6 2069.5 3537.6 1981.6 3406.6
##         Nov    Dec
## 1870 1733.2 1902.2
## 1871 2000.1 1902.0
## 1872 3629.6 4867.8
## 1873 1879.3  885.0
## 1874 2967.0 3058.9
## 1875 2963.5 1653.6
## 1876 3280.4 5125.8
## 1877 4059.8 2959.0
rain %>%  group_by(Year,Month) %>% filter(Station=="Dublin Airport") %>%
  summarise(Rainfall=sum(Rainfall), .groups ="drop") %>% ungroup() %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  dub_ts
rain %>%  group_by(Year,Month) %>% filter(Station=="Belfast") %>%
  summarise(Rainfall=sum(Rainfall), .groups ="drop")  %>% ungroup() %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  bel_ts
rain %>%  group_by(Year,Month) %>% filter(Station=="University College Galway") %>%
  summarise(Rainfall=sum(Rainfall), .groups = "drop") %>% ungroup() %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  ucg_ts
rain %>%  group_by(Year,Month) %>% filter(Station=="Cork Airport") %>%
  summarise(Rainfall=sum(Rainfall), .groups = "drop") %>% ungroup() %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  cor_ts
beldubucgcor_ts <- cbind(bel_ts,dub_ts,ucg_ts,cor_ts)
window(beldubucgcor_ts,c(1850,1),c(1850,12))
##          bel_ts dub_ts ucg_ts cor_ts
## 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
## Jun 1850   62.0   43.6   74.7   11.4
## Jul 1850   96.3   66.0   89.1  179.0
## Aug 1850  110.4   41.2  136.8   46.5
## Sep 1850   65.8   54.2   85.2   40.7
## Oct 1850   87.6   40.4   90.7   53.8
## Nov 1850  104.4   60.0  131.3  153.2
## Dec 1850   57.6   81.1   90.6  169.4

Step 4 : This code helps to plot rainfall data of four different stations in Ireland; Dublin airport, Belfast, University College Galway, Cork airport. It creates an interactive time series graph, and makes the data easier to dissect and make a general analysis.

The main function dygraph() is for creating the graph of the given data. To make this graph more user friendly there is an extra feature added to this graph called dyRangeSelector() which is placed at the nourth end of the graph. This handy feature lets you move the pointer and see the chart from different angles or zoom in/out a date range to identify trends on the chart.

In fact the %>% operator is a link that joins the dygraph() and dyRangeSelector() functions in a fluent way so that when one is called it engages the other. Also, there is an added bonus where one is able to change the width and height of the graph in the width and height respectively.

beldubucgcor_ts %>% dygraph(width=960,height=360) %>%
  dyRangeSelector()

Step 5 : Interactive time series graph with a glimpse of another view.

The following codes were used to create an interactive plot by using dygraph package- The parameters of the ‘dygraph’ function generate a plot object which can further be edited with the help of ‘%>%’ operator as well as the ‘dyRangeSelector’ function. The width of the plot can be specified by using the ‘width’ argument of the ‘dygraph’ function while the height can be set using the ‘height’ argument of the same function. The ‘group’ parameter defines the name of the group to which the series pertains to, and the ’main ‘ parameter defines the title of the plotline. Moreover, the ’dyRangeSelector’function includes a range selector bar on the bottom of the plot so that the user can drag the range of the plot to zoom in and out.

dub_ts %>% dygraph(width=800,height=170,group="dub_belf_ucg_cor",main="Dublin")  %>% dyRangeSelector()
bel_ts %>% dygraph(width=800,height=170,group="dub_belf_ucg_cor",main="Belfast")  %>% dyRangeSelector()
ucg_ts %>% dygraph(width=800,height=170,group="dub_belf_ucg_cor",main="University College Galway")  %>% dyRangeSelector()
cor_ts %>% dygraph(width=800,height=170,group="dub_belf_ucg_cor",main="Cork Airport") %>% dyRangeSelector()

CONCLUSION

Patterns of Rainfall-Fluctuations in rainfall pattern for the four observed stations in Ireland show that there is no enhanced or diminished trend in all the months from year 1850 – 2014. It is however noteworthy to mention that Belfast, University College Galway and Cork Airport record moist climatic condition with more than 100mm of rainfall on a monthly basis than that of Dublin. The highest amount of monthly rainfall was recorded at Cork airport in November 1899 with 460mm, recording got the second highest at Belfast in December 1878 with 329.5mm and the third at UCG in October 1967 with 314.3 and Cork Airport in November 1895 with 312.4mm. It is also noticed that majority of the rainfall occurs in the Autumn season, from September through December first week.