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
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’.
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.
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.
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.