Firstly, a working directory for this exploratory analysis must be set up to facilitate both the importing and exporting of data and results. As well as this, the data required to perform the rainfall analysis must be obtained (from Moodle) and archived for use.

setwd("G:/R assignment Chris")
getwd()
## [1] "G:/R assignment Chris"
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
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.4.3
library(dygraphs)
## Warning: package 'dygraphs' was built under R version 3.4.3

Part 1 - Map of median rainfall in January (1850-2014) for the 25 weather stations in Ireland.

Rainfall map of Ireland

leaflet(data = stations,height = 430,width = 390) %>% addTiles %>% setView(-8,53.5,6) %>%
  addCircleMarkers 
## Assuming 'Long' and 'Lat' are longitude and latitude, respectively
leaflet(data = stations,height = 430,width = 390) %>% addProviderTiles("CartoDB.Positron") %>% 
  setView(-8,53.5,6) %>% addCircleMarkers 
## Assuming 'Long' and 'Lat' are longitude and latitude, respectively

Shapefiles - R data file with county boundaries and settlements for use in ‘leaflet’.

load("Maps.RData")
leaflet(data = counties.spdf,height = 430,width=390) %>% addTiles %>%
  addPolygons(fillOpacity = 0.4,weight = 1,color = "black")
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.4.3

dplyr package

Grouped by station

library(dplyr)
## Warning: package 'dplyr' was built under R version 3.4.3
## 
## 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=median(Rainfall)) -> rain_summary
## Warning: package 'bindrcpp' was built under R version 3.4.3
head(rain_summary)
## # A tibble: 6 x 2
##   Station    mrain
##   <chr>      <dbl>
## 1 Ardara     132  
## 2 Armagh      65.4
## 3 Athboy      70.7
## 4 Belfast     82.1
## 5 Birr        66.2
## 6 Cappoquinn 113

Grouped by month

rain %>% group_by(Month) %>% 
  summarise(mrain=median(Rainfall)) -> rain_months 
head(rain_months)
## # A tibble: 6 x 2
##   Month  mrain
##   <fctr> <dbl>
## 1 Jan    105  
## 2 Feb     74.4
## 3 Mar     73.1
## 4 Apr     62.5
## 5 May     65.5
## 6 Jun     65.2

Multiple groupings - median by month/station combination

rain %>% group_by(Month,Station) %>% 
  summarise(median_rain=median(Rainfall)) -> rain_month_station
head(rain_month_station)
## # A tibble: 6 x 3
## # Groups: Month [1]
##   Month  Station    median_rain
##   <fctr> <chr>            <dbl>
## 1 Jan    Ardara           172  
## 2 Jan    Armagh            75.0
## 3 Jan    Athboy            87.1
## 4 Jan    Belfast          102  
## 5 Jan    Birr              77.5
## 6 Jan    Cappoquinn       147

CONVERTING GROUPS TO JANUARY SPECIFIC DATA

Creating ‘January’ dataset; Median rainfall in 25 stations in January only, 1850-2014.

January <- rain_month_station[1:25, ]

January median rainfall by station

January %>% group_by(Station) %>%
  summarise(mrain=median(median_rain)) -> rain_summary

January median rain by month

January %>% group_by(Month) %>%
  summarise(mrain=median(median_rain)) -> rain_months

January month-station combination

January %>% group_by(Month,Station) %>% 
  summarise(median_rain=median(median_rain)) -> rain_month_station

Joining ‘stations’ to ‘rain_summary’ to get the geographical information.

rain_summary %>% left_join(stations) -> Station_medians
## Joining, by = "Station"

MAPPING

Colour mapping - writing a colour function

colour_fun <- colorNumeric('Blues',Station_medians$mrain)
previewColors (colour_fun,fivenum(Station_medians$mrain))

Colors: colour_fun
Values: fivenum(Station_medians$mrain)

63
92.9
105.7
126.4
177.7

Creating the map

leaflet(data=Station_medians,height=430,width=600) %>% addProviderTiles('CartoDB.Positron')  %>%
  setView(-8,53.5,6) %>% addCircleMarkers(fillColor=~colour_fun(mrain),weight=0,fillOpacity = 0.85) %>%
  addLegend(pal=colour_fun,values=~mrain,title="Jan. median rainfall",position='bottomleft')
## Assuming 'Long' and 'Lat' are longitude and latitude, respectively

Creating rain time series for January medians

rain_month_station$median_rain %>% ts(freq=1,start = 1850) -> Jan_rain_ts 
View(Jan_rain_ts)
####(Jan_rain_ts = time series object).
Jan_rain_ts %>% 
  monthplot(col='dodgerblue',col.base='indianred',lwd.base=3)

‘Monthplot’ function

local_monthplot <- function(station,raindata) {
  raindata %>% filter(Station == station) -> local_rd
  local_rd$Rainfall %>% ts(freq=12,start =1850) -> rain_ts
  rain_ts %>% monthplot(col="dodgerblue",col.base = "indianred",lwd.base = 3)
}

A filename for each station

stations %>% mutate(Filename=paste0('mp',Station,'.png')) ->files
files %>% select(Station,Filename) %>% View

Graphics

leaflet(data=counties.spdf,height=430,width=600) %>% 
  addTiles %>% addPolygons(fillOpacity=0.4,weight=1,color='black',popup='<img src="test.png">')

A monthplot for each station

rain %>% arrange(Year,Month) -> rain2

for (i in 1:nrow(files))
  with(files, {
    png(Filename[i],width=400,height=300)
    local_monthplot(Station[i],rain2)
    dev.off()} )

Pop-ups

files %>% mutate(Popup=paste0('<img src="',Filename,'">')) -> files
files %>% select(Station,Popup) %>% head
## # A tibble: 6 x 2
##   Station     Popup                            
##   <chr>       <chr>                            
## 1 Athboy      "<img src=\"mpAthboy.png\">"     
## 2 Foulksmills "<img src=\"mpFoulksmills.png\">"
## 3 Mullingar   "<img src=\"mpMullingar.png\">"  
## 4 Portlaw     "<img src=\"mpPortlaw.png\">"    
## 5 Rathdrum    "<img src=\"mpRathdrum.png\">"   
## 6 Strokestown "<img src=\"mpStrokestown.png\">"

Final Map

color_fun <- colorNumeric('Blues',station_medians_2$mrain)
leaflet(data=Station_medians,height=430,width=600) %>% addProviderTiles('CartoDB.Positron')  %>%
  setView(-8,53.5,6) %>%
  addCircleMarkers(fillColor=color_fun(Station_medians$mrain),weight=0,
                   fillOpacity = 0.85,popup=station_medians_2$Popup) %>%
  addLegend(pal=color_fun,values=Station_medians$mrain,title="Rainfall",position='bottomleft')
## Warning in color_fun(Station_medians$mrain): Some values were outside the
## color scale and will be treated as NA
## Assuming 'Long' and 'Lat' are longitude and latitude, respectively
## Warning in pal(c(r[1], cuts, r[2])): Some values were outside the color
## scale and will be treated as NA

DISCUSSION OF PATTERNS OF MEDIAN RAINFALL IN JANUARY

The completed map of the 25 weather stations in Ireland shows a distinct pattern. It is evident that the highest median rainfall levels are evident in the south, west and north-west of the country. The dark blue circles show that counties such as Kerry, Cork, Donegal and Waterford have, on average, experienced the highest levels of rainfall over the 164 year period of this rainfall record. The light blue circles show counties with low median rainfall levels, which tend to cluster towards the east of the country (e.g. Dublin, Kildare,Meath). Therefore, rainfall patterns identified on this map suggest the significance of geographic location in understanding rainfall patterns in Ireland.

Part 2 - ‘dygraph’ of the weather stations at Belfast, Dublin Airport, University College Galway, and Cork Airport showing time series of rainfall on a monthly basis.

Creation of time series:

rain %>% group_by(Year,Month) %>% summarise(rf=sum(Rainfall)) -> monthly_total
monthly_total$rf %>% ts(freq=12,start = 1850) -> rain_ts ####(rain_ts = time series object).

‘dygraph’ - A dynamic time series graph.

rain_ts %>% dygraph
rain_ts %>% window(c(1850,1),c(2014,12)) %>% dygraph(width=800,height=300) 

Inclusion of ‘RangeSelector’.

rain_ts %>% dygraph(width=800,height=300) %>% dyRangeSelector

Multiple ‘dygraphs’.

Creating time series for Belfast, Dublin Airport, University College Galway, and Cork Airport.

Dublin Airport:

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

Belfast:

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

University College 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) ->  UCG_ts

Cork Airport:

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

Linked ‘dygraphs’.

dub_ts %>% dygraph(width=800,height=170,group="dub_belf_Cork_UCG",main="Dublin")
bel_ts %>% dygraph(width=800,height=170,group="dub_belf_Cork_UCG",main="Belfast")
Cork_ts %>% dygraph(width = 800,height = 170,group = "dub_belf_Cork_UCG", main = "Cork")
UCG_ts %>% dygraph(width = 800,height = 170,group = "dub_belf_Cork_UCG", main = "UCG") %>%
  dyRangeSelector

Discussion

The use of a dygraph as a way of visually representing rainfall patterns for each of the

four weather stations above provides a valuable tool for seasonal analysis. In particular,

linking the four dygraphs to one another allows the user to observe the individual graphs

at the same time, allowing for easier comparison and identification of rainfall patterns.

Furthermore, the inclusion of a range selector allows for the simultaneous manipulation

of time periods in each dygraph, which ultimatley allows the user to change the observed

time period of each graph in real time i.e. should the user want to look at rainfall for

the years 2000-2014 specifically, he/she may do this with the range selector.

When looking at the data for the four weather stations in question (Cork Airport, Dublin

Airport, Belfast and University College Galway), the dygraph patterns suggest rainfall

Ireland. Both Cork Airport and UCG appear to show higher median levels of rainfall over

the 164 year observation period, which is consistent with observations of high, median

rainfall levels in the South and West of the country, as identified on the earlier maps.

Similarily, the patterns of lower levels of median rainfall in the East and Northeast are

mirrored in the dygraph outputs for Belfast and Dublin Airport, with both of these stations exhibiting lower levels of average rainfall than Cork Airport and UCG.