This function creates a list of ggplot density plots for specified variables by treatment groups.

plot_density_by_treatment(
  data,
  var_map,
  treatment_var,
  show_legend = TRUE,
  theme_use = ggplot2::theme_minimal(),
  ...
)

Arguments

data

A data frame containing the variables to plot and a treatment variable.

var_map

A named list mapping the column names in the data to display names for plotting.

treatment_var

A named vector where the name is the treatment column in the data and the value is the legend title.

show_legend

A logical value indicating whether to show the legend. Defaults to TRUE.

theme_use

ggplot2 theme. Defaults to ggplot2::theme_minimal().

...

Additional arguments to be passed to geom_density.

Value

A list of ggplot objects for each variable in var_map.

Examples

if (FALSE) {
data(mtcars)
data <- mtcars %>% 
  dplyr::select(mpg, cyl) %>% 
  dplyr::rowwise() %>% 
  dplyr::mutate(treatment = sample(c(0,1), 1, replace = TRUE)) %>% 
  dplyr::ungroup()

plots <- plot_density_by_treatment(
  data = data,
  var_map = list("mpg" = "Var 1",
                 "cyl" = "Var 2"),
  treatment_var = c("treatment" = "Treatment Name\nin Legend")
)
}