summary_wind_2d: Summarise y values over binned wind data, split into u and v...

View source: R/summary_wind_2d.R

summary_wind_2dR Documentation

Summarise y values over binned wind data, split into u and v components.

Description

Input data should be original unbinned data including wind direction and wind velocity; binning is done 2-dimensional over cartesian u and v wind vectors

Usage

summary_wind_2d(
  data,
  ws,
  wd,
  z,
  groupings = grp(),
  fun = "mean",
  fun.args = list(),
  nmin = 3,
  ws_max = NA,
  bins = 10^2,
  smooth = TRUE,
  k = 100,
  extrapolate = TRUE,
  dist = 0.1
)

Arguments

data

a data.frame or tibble containing the data (wide format). requires input data including at least three columns carrying information regarding:

  • wind direction (in °)

  • wind velocity

  • z-values (e.g. air pollutant concentration)

ws

symbol giving the wind velocity parameter name (wind velocity preferably in m/s)

wd

symbol giving the wind direction parameter name in degrees

z

symbol giving the parameter name to be summarised

groupings

additional groupings. Use helper grp() to create

fun

function or list of functions for summary.

fun.args

a list of extra arguments to pass to fun.

nmin

numeric, minimum number of values for fun, if n < nmin: NA is returned

ws_max

numeric or Inf, maximum wind velocity for binning: above ws_max, z is set NA

bins

numeric, number of bins over the range of values if !groups %in% c("u", "v")

smooth

TRUE/FALSE, applies if groups = c("u", "v"); should smoothing of summary results should be performed using fit_gam_surface()?

k

numeric, applies if smooth = TRUE; degree of smoothing in smooth term in fit_gam_surface()

extrapolate

TRUE/FALSE, applies if smooth = TRUE; fit_gam_surface() returns extrapolated (predicted) values for u, v coordinates that otherwise would have have NA for summarised z if extrapolate = TRUE, those values are returned (to a certain degree depending on the value of dist)

dist

numeric, fraction of 1, applies if smooth = TRUE and extrapolate = TRUE; maximum distance to next coordinate-pair at which the result of fit_gam_surface(z) should be returned

Value

a tibble with summarised data along u and v wind vectors

Computed variables

  • a tibble is returned, binned over u and v, with variables:

  • wd: wind direction corresponding to midpoint value of u and v

  • ws: wind velocity corresponding to midpoint value of u and v

  • u: midpoints of bins over u (from input wd and ws)

  • v: midpoints of bins over v (from input wd and ws)

  • z: result from fun(z, ...)

Examples

library(ggplot2)
fn <- rOstluft.data::f("Zch_Stampfenbachstrasse_2010-2014.csv")
data <- rOstluft::read_airmo_csv(fn)
data <- rOstluft::rolf_to_openair(data)

# summary NO2
summary_wind_2d(data, ws, wd, NOx, smooth = FALSE)

# multiple stats: Pass function, by name, reference, as function or one sided formula
funs <- list(
  "mean",
  "median" = function(x) median(x, na.rm = TRUE),
  "q95" = ~ stats::quantile(., probs = 0.95)
)

summary_wind_2d(data, ws, wd, NOx, fun = funs, smooth = FALSE)

# is for some reason fun.args used with multiple functions, use ... to catch
# superfluous arguments:
funs <- list(
  "q95" = function(x, ...) stats::quantile(x, probs = 0.95),
  "mean"
)

summary_wind_2d(data, ws, wd, NOx, fun = funs, fun.args = list(na.rm = TRUE),
                smooth = FALSE)

# additional groupings
summary_wind_2d(data, ws, wd, NOx, groupings = grp(site), smooth = FALSE)

# we can use expressions in grp For better readability groupings is
# defined outside of the function call
groupings = grp("site", year = lubridate::year(date))

summary_wind_2d(data, ws, wd, NOx, groupings = groupings, smooth = FALSE)

# smoothing
df1 <- summary_wind_2d(data, ws, wd, NOx, bins = 100^2, smooth = FALSE)
df2 <- summary_wind_2d(data, ws, wd, NOx, bins = 100^2, extrapolate = FALSE)
df3 <- summary_wind_2d(data, ws, wd, NOx, bins = 100^2, smooth = TRUE)

df <- dplyr::bind_rows(
  "smooth = F" = df1,
  "smooth = T, extrapolate = F" = df2,
  "smooth = T, extrapolate = T" = df3,
  .id = "smoothing"
)

ggplot(df, aes(x = u, y = v, fill = NOx)) +
  coord_fixed(expand = FALSE) +
  lims(x = c(-7.5, 7.5), y = c(-7.5, 7.5)) +
  geom_raster() +
  scale_fill_viridis_c(na.value = NA) +
  facet_wrap(vars(smoothing), ncol = 2)

# for a small number of bins reduce k
summary_wind_2d(data, ws, wd, NO2, bins = 5^2, smooth = TRUE, k = 5)

Ostluft/rOstluft.plot documentation built on Jan. 26, 2025, 1:05 a.m.