Creating Dygraph of weather stations

Author

Gertrude Miria

Assignment 2 GY672

Creating a dygraph of 4 time series of weather stations in Ireland

This blog provides the step-by-step processes for creating dygraphs of four weather stations (Belfast, Dublin Airport, University College Galway and Cork Airport showing time series of rainfall on a monthly basis from 1850 to 2015.

Step 1: Load R packages to run these codes

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.3.2
Warning: package 'ggplot2' was built under R version 4.3.2
Warning: package 'tibble' was built under R version 4.3.2
Warning: package 'tidyr' was built under R version 4.3.2
Warning: package 'readr' was built under R version 4.3.2
Warning: package 'purrr' was built under R version 4.3.2
Warning: package 'dplyr' was built under R version 4.3.2
Warning: package 'stringr' was built under R version 4.3.2
Warning: package 'forcats' was built under R version 4.3.2
Warning: package 'lubridate' was built under R version 4.3.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(dygraphs)
Warning: package 'dygraphs' was built under R version 4.3.2
library(sp)
Warning: package 'sp' was built under R version 4.3.2
library(RColorBrewer)
load("X:/Documents/class material/rainfall.RData")

Step 2: Load rainfall data and set working directory

The data file contains monthly rainfall data of 25 rainfall stations in Ireland. The data file has four columns namely “Year”, “Month”, “Rainfall”, and “Stations”. The time series is from 1850 to 2014.

Step 3: Create multiple dygraphs showing monthly time series of 1850.

First group the data by year and month using group_by ()‘, filteres the data by station, then calculate total rainfall for each group using ’summarise()’ and ‘sum()’. After removing the grouping with ‘ungroup()’, keep only the ‘Rainfall’ column with ‘transmute()’. Finally, convert the data to a monthly time series object using ‘ts()’ with a monthly frequency of 12.

Four time series objects are created for four stations ( Dublin Airport, Belfast, University College Galway & Cork Airport), which are then combined using ‘cbind()’. Finally, the ‘window()’ function extracts data for the year 1850, overiding the default grouping behaviour with ‘groups’.

rain %>%  group_by(Year,Month) %>% 
  summarise(Rainfall=sum(Rainfall)) %>% 
  ungroup() %>% transmute(Rainfall) %>% 
  ts(start=c(1850,1),freq=12) -> rain_ts
`summarise()` has grouped output by 'Year'. You can override using the
`.groups` argument.
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)) %>% ungroup() %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  dub_ts
`summarise()` has grouped output by 'Year'. You can override using the
`.groups` argument.
rain %>%  group_by(Year,Month) %>% filter(Station=="Belfast") %>%
  summarise(Rainfall=sum(Rainfall)) %>% ungroup() %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  bel_ts
`summarise()` has grouped output by 'Year'. You can override using the
`.groups` argument.
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
`summarise()` has grouped output by 'Year'. You can override using the
`.groups` argument.
rain %>%  group_by(Year,Month) %>% filter(Station=="Cork Airport") %>%
  summarise(Rainfall=sum(Rainfall)) %>% ungroup() %>% transmute(Rainfall) %>%
  ts(start=c(1850,1),freq=12) ->  cor_ts
`summarise()` has grouped output by 'Year'. You can override using the
`.groups` argument.
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: Create interactive time series graph with four way comparison with rolling mean.

The following codes were used to create an interactive time series graph that displays rainfall data from four different stations in Ireland, namely Dublin Airport, Belfast, University College Galway and Cork Airport. To create the graph, the dygraph() function is used, while the dyRangeSelector() function is used to add a range selector to the bottom of the chart. This helps users to pan and zoom to various date ranges. Additionally, the %>% operator is used to chain the two functions together. Finally, the width and height arguments are used to set the dimensions of the graph.

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

Step 5: Create interactive time series graph with sneak preview of an alternative view.

The following codes were used to create an interactive plot using the ‘dygraph’ package. The ‘dygraph’ function creates a plot object which can be modified using ‘%>%’ operator and the ‘dyRangeSelector’ function. The dimensions of the plot can be set using the ‘width’ and ‘height’ arguments of the ‘dygraph’ function. The ‘group’ argument specifies the name of the group to which the series belongs and the ‘main’ argument specifies the title of the plot. Additionally, the ’dyRangeSelector’function adds a range selector widget to the plot, which allows the user to zoom in and out of the plot.

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()

Rainfall Pattern

Rainfall variability across the four observed stations in Ireland is mostly consistent with no significant increasing or decreasing trend in all months from 1850 - 2014. It is noted that Belfast, University College Galway and Cork Airport experience heavy rainfall of above 100mm monthly compared to Dublin which experience less rainfall. The highest monthly rainfall in Ireland was recorded in the month of November 1899 with 460mm at Cork Airport , followed by Belfast with 329.5mm recorded in December 1878 and UCG with 314.3mm recorded in October 1967 and Cork Airport with 312.4mm in November 1895. It is also observed that most of the rain occur during Autumn in the months of September to early December.