Skip to contents

This function plots densities using bar graphs for categorical variables and actual densities for numeric variables, for several variables (panels) separately by groups (optional; fill color). This is ideal for a combination of categorical, ordinal, and numeric variables, but can also be used with a single type.

Usage

plot_density_by_groups(
  df,
  vars = NULL,
  group = NULL,
  fix_scales = FALSE,
  min_values_to_treat_as_numeric = 12
)

Arguments

df

data.frame

vars

variables to plot (if NULL, all variables except group)

group

grouping variable (if NULL, all rows are assumed to belong to the same group)

fix_scales

(logical) whether to keep same scales for all variables (default FALSE)

min_values_to_treat_as_numeric

(numeric) number of distinct values for a numeric variable to be treated as continuous

Value

ggplot2 plot

Details

Non-numeric variables are always treated as such and get a bar graph. Numeric variables can get either a bar graph (when the number of unique values excluding NA is < min_values_to_treat_as_numeric), or a density plot.

Density for categorical variables is calculated as frequency/total for each distinct value, separately by groups.

Examples

if (FALSE) {
df |> plot_density_by_groups()
df |> plot_density_by_groups(group="Gender")
}