Skip to contents

This function creates a ggplot visualization of a decision tree from Margot Causal Forest results. It handles label transformations, includes both standardized and original split values, and provides informative feedback using cli messages.

Usage

margot_plot_decision_tree(
  result_object,
  model_name = NULL,
  original_df = NULL,
  x_padding = 0.12,
  y_padding = 0.12,
  border_size = 0.5,
  text_size = 4,
  edge_label_offset = 0.025,
  span_ratio = 0.4,
  non_leaf_fill = "lightyellow",
  title = NULL,
  leaf_palette = NULL,
  remove_tx_prefix = TRUE,
  remove_z_suffix = TRUE,
  use_title_case = TRUE,
  remove_underscores = TRUE,
  remove_action_label = TRUE,
  label_mapping = NULL
)

Arguments

result_object

A list containing the policy tree object or Margot results.

model_name

Character string specifying the model name in the results object. Default is NULL.

original_df

Optional dataframe with untransformed variables, used to display split values on the data scale.

x_padding

Numeric value for horizontal padding of the plot. Default is 0.12.

y_padding

Numeric value for vertical padding of the plot. Default is 0.12.

border_size

Numeric value for the size of node borders. Default is 0.5.

text_size

Numeric value for the size of text in the plot. Default is 3.

edge_label_offset

Numeric value for the offset of edge labels. Default is 0.025.

span_ratio

Numeric value for the aspect ratio of the plot. Default is 0.4.

non_leaf_fill

Character string specifying the fill color for non-leaf nodes. Default is "lightyellow".

title

Character string for the plot title. Default is NULL.

leaf_palette

Vector of colors for leaf nodes. Default is NULL (uses Okabe-Ito palette).

remove_tx_prefix

Logical indicating whether to remove "tx_" prefix from labels. Default is TRUE.

remove_z_suffix

Logical indicating whether to remove "_z" suffix from labels. Default is TRUE.

use_title_case

Logical indicating whether to convert labels to title case. Default is TRUE.

remove_underscores

Logical indicating whether to remove underscores from labels. Default is TRUE.

remove_action_label

Logical indicating whether to remove "Action: " prefix from leaf node labels in the final plot. Default is TRUE.

label_mapping

Optional named list for custom label mappings. Keys should be original variable names (with or without "model_" prefix), and values should be the desired display labels. Default is NULL.

Value

A ggplot object representing the decision tree.

Examples

if (FALSE) { # \dontrun{
# Assuming 'results' is your Margot results object and 'model_name' is the name of your model
tree_plot <- margot_plot_decision_tree(results, model_name = "my_model")
print(tree_plot)

# To keep the "Action: " prefix in leaf node labels
tree_plot <- margot_plot_decision_tree(results, model_name = "my_model", remove_action_label = FALSE)
print(tree_plot)

# Using custom label mapping and original_df for unstandardized values
label_mapping <- list(
  "t2_env_not_env_efficacy_z" = "Deny Personal Environmental Efficacy",
  "t2_env_not_climate_chg_real_z" = "Deny Climate Change Real"
)
tree_plot <- margot_plot_decision_tree(results, model_name = "model_t2_env_not_env_efficacy_z",
                                       label_mapping = label_mapping,
                                       original_df = original_df)
print(tree_plot)
} # }