library(dplyr)
library(tidyr)
library(xts)
library(dygraphs)
load("rainfall.RData")
stations_use <- c("Belfast", "Dublin Airport", "University College Galway", "Cork Airport")
rain_4 <- rain %>%
filter(Station %in% stations_use) %>%
summarise(Rainfall = sum(Rainfall), .by = c(Station, Year, Month)) %>%
mutate(Date = as.Date(sprintf("%d-%02d-01", Year, Month)))
rain_wide <- rain_4 %>%
select(Date, Station, Rainfall) %>%
pivot_wider(names_from = Station, values_from = Rainfall) %>%
arrange(Date)
rain_xts <- xts(rain_wide %>% select(-Date), order.by = rain_wide$Date)
colnames(rain_xts) <- c("Belfast", "Dublin", "UCG", "Cork")
cb_cols <- c("#0072B2", "#D55E00", "#009E73", "#CC79A7")
start_view <- as.Date("1950-01-01")
end_view <- max(index(rain_xts))
# Mean check over the SAME window as the plot (calculated but NOT printed)
rain_window <- rain_xts[paste0(start_view, "/", end_view)]
mean_rain <- round(colMeans(rain_window, na.rm = TRUE), 1)
invisible(mean_rain)
dygraph(rain_xts, main = "Monthly Rainfall - Belfast, Dublin, UCG, Cork") %>%
dyAxis("y", label = "Rainfall (mm)") %>%
dyOptions(colors = cb_cols, drawPoints = FALSE, fillGraph = FALSE, strokeWidth = 1) %>%
dyLegend(show = "onmouseover") %>%
dyRangeSelector(dateWindow = c(start_view, end_view)) %>%
dyRoller(rollPeriod = 12, showRoller = FALSE)Interactive Comparison of Monthly Rainfall at Four Irish Stations Using dygraphs
Overview
I used this blog to build an interactive dygraph looking at monthly rainfall for four Irish stations: Belfast, Dublin Airport, University College Galway, and Cork Airport. I mainly wanted a straightforward way to see how rainfall differs around the country and whether those differences hold up over long periods. Using dygraphs made that much easier, especially the RangeSelector, which lets all four series move together so wet and dry spells can be compared directly. The goal wasn’t heavy statistics, just to create a clear visual tool that lets the data speak for itself.
Data used
- Dataset:
rainfall.RData(from the GY672 class materials) - Objects:
rain(monthly rainfall) andstations(metadata) - Variable: monthly rainfall totals (mm)
- Time span: varies by station, roughly mid-1800s to 2014
- Each observation is indexed by Year and Month
Code
Brief Discussion of Patterns
A quick mean check suggests Dublin is highest on average, Cork close behind, Belfast lower, and UCG lowest.
What I like about the dygraph is you can actually see that play out when you zoom around: UCG sits under the other three a lot of the time, while Dublin and Cork often track fairly close together. Belfast usually lands somewhere in the middle.
It’s a noisy series overall (very “Irish” climate), so the 12-month roller helps a lot - it smooths the clutter and makes the longer wet/dry spells easier to spot, especially around the late 1970s and early 2000s. The RangeSelector is the real win though: you can drag to a decade and compare stations properly without making a pile of separate plots.
If I was taking it further, I’d look at seasonal totals (winter vs summer) or plot anomalies relative to 1981-2010 to see if the station gaps change through the year.