Particularly in exploratory data analysis, it can be instructive to see histograms or density charts. This function works with both regular data frames and survey design objects, properly accounting for survey weights when present.

plot_distributions(
  data,
  var_names = NULL,
  plot_type = c("auto", "histogram", "density"),
  hist_align_y = FALSE,
  plot_theme = NULL
)

Arguments

data

A dataframe or survey design object. If var_names is NULL, all numeric variables in data will be used, otherwise those included in var_names will be selected. For survey design objects, survey weights will be properly incorporated using svyhist() and svysmooth().

var_names

A named character vector with new variable names or a tibble as provided by get_rename_tribbles() If provided, only variables included here will be plotted. Apart from that, this will only determine the names of the list items, so it is most relevant if the output is to be combined with a correlation matrix, e.g., from cor_matrix()

plot_type

Type of plot that should be produced - histogram or density plot. If auto, histograms are produced for variables that take fewer than 10 unique values, density plots for others. If a number is provided, that number is used as the maximum number of unique values for which a histogram is used.

hist_align_y

Should histograms use the same y-axis, so that bin heights are comparable? Defaults to FALSE

plot_theme

Additional theme_ commands to be added to each plot

Value

A list of plots

Examples

if (FALSE) { # \dontrun{
# Regular data
plot_distributions(mtcars, var_names = c(wt = "Weight", mpg = "Efficiency",
                                         am = "Transmission", gear = "Gears"))

# Survey data
library(survey)
library(srvyr)
data(api)
dstrat <- apistrat %>% as_survey_design(1, strata = stype, fpc = fpc, weight = pw)
plot_distributions(dstrat, var_names = c(enroll = "Enrollment", api00 = "API Score"))
} # }