Good morning everyone, what’s up brother

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 'stringr' 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.3     ✔ 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)

library(tmap)
## Warning: package 'tmap' was built under R version 4.3.2
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.2
library(sf)
## Warning: package 'sf' was built under R version 4.3.2
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE

In this code snippet, we begin by setting up our R environment and loading the necessary libraries. Each library serves a specific purpose in the subsequent steps. The tidyverse, dplyr, and sf libraries are essential for data manipulation and transformation, while dygraphs is crucial for creating dynamic time series graphs. Additionally, sp, RColorBrewer, tmap, and leaflet will be used for spatial data processing and map visualization.

Now, loading the rainfall data and preparing it for mapping.

load("rainfall.RData")
rain
## # A tibble: 49,500 × 4
##     Year Month Rainfall Station
##    <dbl> <fct>    <dbl> <chr>  
##  1  1850 Jan      169   Ardara 
##  2  1851 Jan      236.  Ardara 
##  3  1852 Jan      250.  Ardara 
##  4  1853 Jan      209.  Ardara 
##  5  1854 Jan      188.  Ardara 
##  6  1855 Jan       32.3 Ardara 
##  7  1856 Jan      152.  Ardara 
##  8  1857 Jan      179.  Ardara 
##  9  1858 Jan      110.  Ardara 
## 10  1859 Jan      158.  Ardara 
## # ℹ 49,490 more rows
# step 1-dplyr
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

In this step, the dplyr package is used to aggregate the rainfall data. The group_by function groups the data by Year and Month, and summarise calculates the total rainfall for each combination of Year and Month. The ungroup function removes grouping, and transmute retains only the Rainfall column. The resulting data is then transformed into a time series object (ts) with a specified start date and frequency (monthly in this case). Finally, the window function is used to focus on a specific time range (1870-1877).

#step 2-dynamic time series graph
library(dygraphs) # A dynamic graph library - you will need to install it first time
rain_ts %>% dygraph()  # Try moving the pointer along the curve

This step introduces the dygraphs library, a dynamic graphing tool. The dygraph function is applied to the rain_ts time series object, creating an interactive time series graph. Users can move the pointer along the curve to explore the data points.

#step 3-dynamic time series graph with a window 
rain_ts %>% 
  window(c(1850, 1), c(1889, 12)) %>%
  dygraph(height = 300, width = 960)

Building upon the previous step, this code introduces a window restriction to focus on a specific time range (1850-1889). The resulting dygraph is limited to this time window, providing a more detailed view of the selected period.

#step 4-an interactive window
rain_ts %>% dygraph() %>% 
  dyRangeSelector()

This step adds interactivity to the dygraph by implementing a range selector (dyRangeSelector). Users can drag and adjust the selected time window, allowing for a more dynamic exploration of the data.

#step 5-interactive rolling mean
rain_ts %>% dygraph(width = 960, height = 330) %>% 
  dyRangeSelector() %>% dyRoller(rollPeriod = 600)

Here, a rolling mean is added to the interactive dygraph using the dyRoller function. The rolling mean smoothens the curve and helps identify trends by averaging over a specified rolling period (600 months in this case).

#step 6-multiple dygraphs
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

This step involves creating separate time series objects (dub_ts, bel_ts, ucg_ts, cor_ts) for each weather station. These are then combined into a single object (beldubucgcor_ts) for comparative analysis. The window function is applied to focus on a specific time range (1850-1850).

#step 7-multiple dygraph-4 way comparison with rolling mean 
beldubucgcor_ts %>% dygraph(width = 960, height = 360) %>%
  dyRangeSelector()

The combined time series object is visualized using a dygraph for a four-way comparison. The dygraph includes a range selector for interactive exploration.

#step 8-sneak preview of an alternative view
dub_ts %>% dygraph(width = 800, height = 130, group = "dub_belf_ucg_cor", main = "Dublin")
bel_ts %>% dygraph(width = 800, height = 130, group = "dub_belf_ucg_cor", main = "Belfast")
ucg_ts %>% dygraph(width = 800, height = 130, group = "dub_belf_ucg_cor", main = "University College Galway")
cor_ts %>% dygraph(width = 800, height = 170, group = "dub_belf_ucg_cor", main = "Cork Airport") %>% dyRangeSelector()

In this final step, individual dygraphs are created for each station, providing a detailed view of their respective rainfall patterns. The dyRangeSelector is applied to maintain interactivity across all the dygraphs.

These steps collectively form a comprehensive exploration of the rainfall time series data, utilizing interactive maps and dygraphs for in-depth analysis and visualization.