Skip to contents

Bins the target variable and plots the total exposure per bin as a bar chart — an exposure-weighted distribution of the target. Shares its argument conventions, binning, NA handling, split behaviour, and plotly styling with one_way(); the only difference is that the target itself is the x-axis variable, so there is no obs argument.

Usage

distribution(data, ...)

# Default S3 method
distribution(
  data,
  var = "target",
  exposure = "exposure",
  split = NA_character_,
  bins = 35L,
  time_unit = NA_character_,
  type_agg = c("equal_exposure", "equal_range"),
  ret = c("plot", "data"),
  verbose = FALSE,
  ...
)

Arguments

data

A data frame or data.table. Must contain all columns referenced by other arguments.

...

Arguments passed to methods.

var

Target column whose distribution to plot. Bare name or string. Default "target".

exposure

Column of exposure weights. If the column does not exist in data, every row is given weight 1. Bare name or string. Default "exposure".

split

Optional column to group bars by. Bare name, string, or NA / NULL for no split. Default NA.

bins

[integer(1)] Number of equal-exposure bins for numeric targets with more than bins unique values. Default 35.

time_unit

[character(1)] For Date / POSIXct targets, the width of each bin (passed to base::cut.POSIXt()). Default NA.

type_agg

[character(1)] Binning strategy for numeric targets: "equal_exposure" (default) or "equal_range".

ret

[character(1)] "plot" (default) returns a plotly object; "data" returns the aggregated data.table.

verbose

[logical(1)] Report how many NA values of var were moved to the trailing "NA" category? Default FALSE.

Value

A plotly object, or a data.table when ret = "data", or NULL with a warning if the plot cannot be produced.

Details

Column name arguments (var, exposure, split) accept both bare (unquoted) names and strings, so distribution(df, mpg) and distribution(df, "mpg") are equivalent.

Examples

# \donttest{
distribution(mtcars, mpg, bins = 10)
distribution(mtcars, var = "mpg", split = "am")
# }