---
title: "Decision Tree Example"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{decision-tree-example}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup, warning=FALSE, message=FALSE}
library(ggflowchart)
library(dplyr)
```

# Define input data 

The main input data is a two column data frame or tibble of edges. It must have two columns, with the first column containing node names for the "from" nodes, and the second containing node names for the "to" nodes. Node names must be unique.

```{r data}
goldilocks <- tibble::tibble(
  from = c(
    "Goldilocks",
    "Porridge", "Porridge", "Porridge",
    "Just right",
    "Chairs", "Chairs", "Chairs",
    "Just right2",
    "Beds", "Beds", "Beds",
    "Just right3"
  ),
  to = c(
    "Porridge",
    "Too cold", "Too hot", "Just right",
    "Chairs",
    "Still too big", "Too big", "Just right2",
    "Beds",
    "Too soft", "Too hard", "Just right3",
    "Bears!"
  )
)
```

Optionally, also define additional node attributes. 

```{r node-data}
node_data <- tibble::tibble(name = c(
  "Goldilocks", "Porridge", "Just right", "Chairs",
  "Just right2", "Beds", "Just right3", "Too cold",
  "Too hot", "Still too big", "Too big", "Too soft",
  "Too hard", "Bears!"
)) %>%
  dplyr::mutate(label = gsub("\\d+$", "", name))
```

Create the flowchart using the `ggflowchart()` function.

```{r flowchart}
ggflowchart(goldilocks, node_data)
```

We can also specify a column in `node_data` we want to colour the nodes based on: 

```{r flowchart-fill-columm}
node_data <- node_data %>%
  dplyr::mutate(
    type = c("Character", "Question", "Answer",
             "Question", "Answer", "Question",
             "Answer", "Answer", "Answer",
             "Answer", "Answer", "Answer",
             "Answer", "Character")
    )
```

and pass this to the `fill` argument:

```{r flowchart-fill}
ggflowchart(goldilocks, node_data, fill = type)
```

Note that the column name can be passed either quoted or unquoted e.g., `ggflowchart(goldilocks, node_data, fill = "type")` will also work. If single colour is passed instead (in quotes e.g., `"blue"`) and this is also the name of a column, it will assume you want to use the values in the column. If that isn't the case, please rename the column.

Optionally, add some styling using the {ggplot2} `theme()` function and title using the `labs()` function.

```{r flowchart-style}
library(ggplot2)
library(rcartocolor)
ggflowchart(goldilocks,
  node_data,
  fill = type,
  colour = "#585c45",
  text_colour = "#585c45",
  arrow_colour = "#585c45"
) +
  scale_x_reverse() +
  scale_fill_carto_d(palette = "Antique") +
  labs(title = "The Goldilocks Decision Tree") +
  theme(
    legend.position = "none",
    plot.background = element_rect(
      colour = "#f2e4c1",
      fill = "#f2e4c1"
    ),
    plot.title = element_text(
      size = 20,
      hjust = 0.5,
      face = "bold",
      colour = "#585c45"
    )
  )
```
