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
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 graphlibrary(dygraphs) # A dynamic graph library - you will need to install it first timerain_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.
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 meanrain_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.
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.