dygraph of the weather stations

——-yangteng

The precipitation map of dynamic time series can directly obtain the key information of precipitation patterns in relevant regions, which has important reference significance for us to study climate change, meteorology and precipitation. Through the dynamic time series precipitation map, we can know the long-term precipitation trend of this region and build a model to predict the future precipitation. We can also analyze whether there are extreme precipitation events in the area and take protective measures. We can also plan water use based on precipitation to ensure a sustainable water supply. Today, we will use R studio software to Creat a dygraph of the weather stations at Belfast, Dublin Airport, University College Galway, and Cork Airport showing time series of rainfall on a monthly basis. Include a RangeSelector control that simultaneously changes the time window on the four time series.

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 '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.0
✔ 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)
# step 1-dplyr
load("~/GY688a/r studio/rainfall.RData")
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

We use codes to group the data by year and month, and calculate the sum of rainfall in each group, we convert the data into a time series and select a specific time window in the selected time series, that is, from January 1870 to December 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

We use this code to create a dynamic time series plot, moving the mouse pointer along the curve to interactively see changes in the time series data. We can intuitively see the relationship between precipitation and time through the line chart.

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

We use this code to create a dynamic time series plot, moving the mouse pointer along the curve to interactively see changes in the time series data. We can intuitively see the relationship between precipitation and time through the line chart.

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

We use the dyRangeSelector() code to insert a range selector into the time series graph. The user can drag the range selector with the mouse to see the time range of interest.

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

We use the “dyRoll” code to create an interactive time series plot in R with a range selector and rolling mean, where the user can select the time range and the rolling mean is displayed.

#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

We use the group and summarise codes to calculate the total monthly precipitation for the four regions We use the ungroup code to ungroup the data box We used the transmute code to extract precipitation data for each site We use the ts code to convert the function data into a time series We use cbind to combine the four time series into a single data frame

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

This code will generate an interactive time series graph containing precipitation time series data for the four sites “Belfast”, “Dublin Airport”, “University College Galway” and “Cork Airport”. The user can use the range selector to scale and select the time range of interest.

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