Introduction

The aim of this exercise was to create a monthly rainfall dygraph showing a series of years, 1850-2014, for 4 individual weather stations in 4 different locations in Ireland. The weather stations used were Belfast, Dublin Airport, Cork Airport and University College Galway.

Data

The data used for this exercise was provided by Dr.Conor Murphy and Simon Noone from Maynooth University. The data comes from Met Eireann and was used in conjuction with Simon Noone’s 2016 PHD thesis that analysed long term precipitation records over the Island of Ireland. The weather stations used represent Ireland from North to South and East to West.

Code

Visualilzing data with tables

The rainfall data noted in the above sections was downloaded from moodle and saved as an R file. This was the first step in creating the dygraph. To better visualise the data the function head(stations) was used to show data for the first 6 stations and the 9 other pieces of information that are were recorded, these include elevation longitude and latitude. Head(rain) produces a similar table however the data shown is different. Here a rainfall for the first 6 recorded years is shown along with the station. This reduces the table to 6 rows and 2 columns rather that the previous 9 columns.

load('rainfall.RData')
head(stations)
##       Station Elevation Easting Northing   Lat  Long    County
## 1      Athboy        87  270400   261700 53.60 -6.93     Meath
## 2 Foulksmills        71  284100   118400 52.30 -6.77   Wexford
## 3   Mullingar       112  241780   247765 53.47 -7.37 Westmeath
## 4     Portlaw         8  246600   115200 52.28 -7.31 Waterford
## 5    Rathdrum       131  319700   186000 52.91 -6.22   Wicklow
## 6 Strokestown        49  194500   279100 53.75 -8.10 Roscommon
##   Abbreviation      Source
## 1           AB Met Eireann
## 2            F Met Eireann
## 3            M Met Eireann
## 4            P Met Eireann
## 5           RD Met Eireann
## 6            S Met Eireann
head(rain)
##   Year Month Rainfall Station
## 1 1850   Jan    169.0  Ardara
## 2 1851   Jan    236.4  Ardara
## 3 1852   Jan    249.7  Ardara
## 4 1853   Jan    209.1  Ardara
## 5 1854   Jan    188.5  Ardara
## 6 1855   Jan     32.3  Ardara

Using dplyr library

The dplyr package was ran. This helps to manipulate and vision the data. It constrains options and provides verbs that correspond to functions. This allows ideas and thoughts to be constructed into code (CRAN,2018). The function %>% works as a pipeline for dplyr and it makes it more simple to write and maintain code in R. Below is a table that has stations and rainfall for the first 6 stations.

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
rain %>% group_by(Station) %>% 
summarise(mrain=mean(Rainfall))  -> rain_summary
head(rain_summary)
## # A tibble: 6 x 2
##   Station    mrain
##   <chr>      <dbl>
## 1 Ardara     140. 
## 2 Armagh      68.3
## 3 Athboy      74.7
## 4 Belfast     87.1
## 5 Birr        70.8
## 6 Cappoquinn 121.

Creating graphs

After constructing tables that help visualize the data in words rather than code, simple graphic visualizations can be carried out. Barplots are among the simplest form of graphics but give a clear representation of data. Below is a barchart for rain and months.

rain %>% group_by(Month) %>% 
summarise(mrain=mean(Rainfall)) -> rain_months
head(rain_months)
## # A tibble: 6 x 2
##   Month mrain
##   <fct> <dbl>
## 1 Jan   113. 
## 2 Feb    83.2
## 3 Mar    79.5
## 4 Apr    68.7
## 5 May    71.3
## 6 Jun    72.7
barplot(rain_months$mrain,names=rain_months$Month,las=3,col='red')

Graphs for the total rainfall over a period 1850-2014 can also be produced.Within this then graphs for individual locations can be created.Below is total rainfall over the 165 year period and individual stations Dublin Airport, Belfast, University College Galway and Cork Airport respectively.

rain %>% group_by(Year) %>%
summarise(total_rain=sum(Rainfall)) -> rain_years
with(rain_years,plot(Year,total_rain,type='l',col='green'))

rain %>% group_by(Year) %>% 
filter(Station=='Dublin Airport') %>%
summarise(total_rain=sum(Rainfall)) -> rain_years_str
with(rain_years_str,plot(Year,total_rain,type='l',col='blue'))

rain %>% group_by(Year) %>% 
filter(Station=='Belfast') %>%
summarise(total_rain=sum(Rainfall)) -> rain_years_str
with(rain_years_str,plot(Year,total_rain,type='l',col='purple'))

rain %>% group_by(Year) %>% 
filter(Station=='University College Galway') %>%
summarise(total_rain=sum(Rainfall)) -> rain_years_str
with(rain_years_str,plot(Year,total_rain,type='l',col='black'))

rain %>% group_by(Year) %>% 
filter(Station=='Cork Airport') %>%
summarise(total_rain=sum(Rainfall)) -> rain_years_str
with(rain_years_str,plot(Year,total_rain,type='l',col='orange'))

Dygraphs

A dygraph is an interactive time series plot that allows the user to focus on different periods within a long set of years.Several different time series can be examined at the same time (Vanderkam,2018). Here a dygraph was created to represent a 165 year period. This is useful as it removes the need for numerous graphs and makes data investigation far more time efficient. Dygraphs can include a range selector which is the accessory that allows certain time windows to be researched without having to produce another graph. Below is a dygraph that has the combined rainfall for 4 stations on the Island of Ireland. These stations are Belfast,Dublin Airport, University College Galway and Cork Airport. These 4 stations give a good spatial representation of rainfall around the Island of Ireland as they cover North to South and East to West.

rain %>% group_by(Year,Month) %>% summarise(rf=sum(Rainfall)) -> monthly_total
plot(monthly_total$rf[1:120],type='b',ann=F, axes=F, cex=0.6)
axis(1,at=seq(0,120,by=12),labels=1850:1860); axis(2); title('Total Irish Rainfall 1850-1860')

monthly_total$rf %>% ts(freq=12,start=1850) -> rain_ts
rain_ts %>% window(c(1850,1),c(1853,12))
##         Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct
## 1850 2836.3 2158.9  964.1 3457.2 1492.1 1362.4 2584.8 1906.4 1763.1 1567.8
## 1851 4875.5 1379.9 1997.9 1368.1 1124.2 2537.1 2258.3 2416.2 1234.5 2834.2
## 1852 4036.1 2311.2 1116.0 1110.0 1754.6 4484.9 1716.0 2752.0 1433.1 2217.7
## 1853 3618.6 1073.9 2088.0 1881.9  829.1 1936.7 2512.3 2072.0 1504.1 4627.6
##         Nov    Dec
## 1850 2828.5 2600.1
## 1851 1134.3 1719.2
## 1852 5581.2 5205.9
## 1853 2651.1 1209.3
library(dygraphs)
rain_ts %>% dygraph

Above is a dygraph for rainfall across the time period of 1850-2014. This graph shows the trouble with producing dygraphs for large time series and the need for a range selector is evident.

Individual Dygraphs with Range Selector

Dygrpahs were produced for rainfall at each individual weather station. Each dygraph contains a range selector that allows specific points in time to be observed effectively. To do this each individual weather station was grouped by Year and Month and then filtered by it’s station name i.e Dublin Airport. The next step requires rainfall data to be ungrouped by months and years and uses the transmute function aswell to allow one variable to be used, in this case rainfall. Finally a time series window is used to create a start point for the dygraph by ts(start=c(1850,1),freq=12) and let it be called something reliable like -> Bel for Belfast.Below shows the procedure of filtering stations and naming those stations appropriatly.

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

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

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

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

Each individual dygraph below contains a range selector, along with a title showing which stations the data belongs too. Each dygraph can be manipulated in height and width if necessary and the range selector creates an interactive and dynamic option for exploring specific time periods within this data.

gal_ts %>% dygraph(width=800,height=360,main="Galway") %>% dyRangeSelector
bel_ts%>% dygraph(width=800,height=360,main="Belfast") %>% dyRangeSelector
dub_ts%>% dygraph(width=800,height=360,main="Dublin") %>% dyRangeSelector
cork_ts%>% dygraph(width=800,height=360,main="Cork") %>% dyRangeSelector

Combinding multiple stations into a single dygraph

In order to create the combined station dygraph a binding function is needed. This binds the 4 seperate station shown in above into a single interactive graph. Shown below is a table of rainfall at each station. This includes months January to December for the year 1850. Within the code below the timeframe for this can be changed. This is achieved by changing c(1850,12) to (‘whatever year you see fit’,‘an ending month of your choice’). An example might be window(beldubcorkgal_ts,c(1850,1),c(1855,6)).

beldubcorkgal_ts <- cbind(bel_ts,dub_ts,cork_ts,gal_ts)
window(beldubcorkgal_ts,c(1850,1),c(1850,12))
##          bel_ts dub_ts cork_ts gal_ts
## Jan 1850  115.7   75.8   155.3  108.9
## Feb 1850  120.5   47.8    92.6  131.5
## Mar 1850   56.8   18.5    56.0   56.6
## Apr 1850  142.6   97.5   207.2  120.5
## May 1850   57.9   58.6    35.3   69.8
## Jun 1850   62.0   43.6    11.4   74.7
## Jul 1850   96.3   66.0   179.0   89.1
## Aug 1850  110.4   41.2    46.5  136.8
## Sep 1850   65.8   54.2    40.7   85.2
## Oct 1850   87.6   40.4    53.8   90.7
## Nov 1850  104.4   60.0   153.2  131.3
## Dec 1850   57.6   81.1   169.4   90.6

Below is the combined 4 station rainfall dygraph for Dublin Airport, Belfast, University College Galway and Cork Airport.A range selector is also added making investigation into multiple years at once more accessible.The 4 shortened station names chosen to use in the r code under ‘individual stations’, i.e bel or dub, are put together to create the single dygraph. The width and height of the graph can be manipulated. The name of each station involved is shown in the top right hand corner and the colour scheme of these names matches the plot on the graph.

beldubcorkgal_ts %>% dygraph(width=800,height=360) %>% dyRangeSelector

Patterns and Analysis

From the above dygraphs some general analysis can be carried out. The range selector allows for greater resolution observations. The wettest record along the graph was in Cork. This large increase in rain fell in December 1899. The rainfall recorded in this month for Cork was triple the amount recorded for Galway and Belfast and was increasingly over 4 times the amount in recorded in Dublin. The wettest year recorded seems to be 1872 where significant amounts of rainfall were recorded among summer months.Cork recorded the most rainfall in July. The autumn and winter months had high amounts of rainfall also. The driest year recorded is 1887. The records for Galway where the highest in the year and the most rain fell in September, measuring 206.7mm. From the individual dygraphs a clear distinction between rainfall appears when comparing Cork and Dublin. Cork is significantly wetter than Dublin. When analysing Galways rainfall data, the dygraph shows a slight rise in high rainfall days. Overall the dygraphs above show a variety of variation and correspond to the findings of Noone and Murphy.

Bibliogrpahy

Gravitas (2016) magrittr: Simplifying R code with pipes https://blog.revolutionanalytics.com/2014/07/magrittr-simplifying-r-code-with-pipes.html.

Noone, S. (2016)Development and analysis of a homogeneous longterm precipitation network (1850-2015) and assessment of historic droughts for the island of Ireland. http://eprints.maynoothuniversity.ie/8147/1/Simon%20Noone%20PhD%20thesis%202016.pdf.

Vanderkam, D. (2018) Interface to ‘Dygraphs’ Interactive Time Series Charting Library. https://cran.r-project.org/web/packages/dygraphs/dygraphs.pdf.

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.