library(tidyverse) library(lubridate) library(openair) library(threadr) library(leaflet) # store the filename in a variable filename <- "https://data.rivm.nl/data/luchtmeetnet/Vastgesteld-jaar/2019/2019_NO2.csv" mydata <- read.table(file = filename, header = TRUE, sep = ";", dec = ".", comment.char = "#", quote = "\"") |> dplyr::mutate(date = as.POSIXct(begindatumtijd, format = "%Y-%m-%dT%H:%M:%S", tz="Etc/GMT-1"), date_end = as.POSIXct(einddatumtijd, format = "%Y-%m-%dT%H:%M:%S", tz="Etc/GMT-1")-1) |> threadr::time_pad(interval = "hour", by = "meetlocatie_id") # show contents of the variable head(mydata) # Adding other data is easy, for example from a different matrix # set the filename filename <- "https://data.rivm.nl/data/luchtmeetnet/Vastgesteld-jaar/2019/2019_groep_HC_aerosol.csv" mydata2 <- read.table(file = filename, header = TRUE, sep = ";", dec = ".", comment.char = "#", quote = "\"") |> dplyr::mutate(date = as.POSIXct(begindatumtijd, format = "%Y-%m-%dT%H:%M:%S", tz="Etc/GMT-1"), date_end = as.POSIXct(einddatumtijd, format = "%Y-%m-%dT%H:%M:%S", tz="Etc/GMT-1")-1) |> threadr::time_pad(interval = "day", by = c("meetlocatie_id", "component")) # combine the data into one file is easy, as all files have the same structure: mydata <- dplyr::bind_rows(mydata, mydata2) # cleanup some of the column names, so we can easily use other packages to plot # the data mydata <- mydata |> dplyr::rename(site = meetlocatie_id, variable = component, value = waarde) # Since the data is now in long, we can use the R package threadr to plot the # time variation as a ggplot: threadr::plot_time_variation(df = mydata |> filter(site == "NL10641", variable == "NO2"), colours = "steelblue", legend_name = openair::quickText("NO2")) # ggplot is really nice, as it allows for modification after creating the # initial plot. However, if we want to use openair, then we need to do a minor # modification to the data structure: mydata_openair <- mydata |> tidyr::pivot_wider(names_from = variable, values_from = value) openair::timeVariation(mydata = mydata_openair |> filter(site == "NL10641"), pollutant = "NO2") # Lets add some meta data # set the filename filename <- "https://data.rivm.nl/data/luchtmeetnet/luchtmeetnet_meetlocaties.csv" meta_sites <- read.table(file = filename, header = TRUE, sep = ";", dec = ".", comment.char = "#", quote = "\"") |> dplyr::mutate(begindatum_locatie = as.POSIXct(begindatum_locatie, format = "%Y-%m-%dT%H:%M:%S", tz="Etc/GMT-1"), einddatum_locatie = as.POSIXct(einddatum_locatie, format = "%Y-%m-%dT%H:%M:%S", tz="Etc/GMT-1")-1) # show what we have head(meta_sites) # We changed the names of the columns in the mydata, so we need to do the same # here. And in this case we are only interested in the coordinates. meta_sites <- meta_sites |> dplyr::rename(site = meetlocatie_id, lat = breedtegraad, lon = lengtegraad) %>% dplyr::select(site, lat, lon) # Join the two together, using a left_join to retain all the measurements: mydata <- dplyr::left_join(mydata, meta_sites, by = join_by(site)) # plot all the sites in the data frame using leaflet leaflet::leaflet(data = mydata |> select(site, lat, lon) |> distinct()) |> leaflet::addTiles() |> leaflet::addMarkers(~lon, ~lat, popup = ~site, label = ~site)