# Intro to fractalforest

The `fractalforest` package allows the simulation of fractal trees and, consequently, fractal forests. The trees are created using the Lindenmayer system (L-system).

It is possible to use forest inventory, floristic surveys, and phytosociological studies databases to represent the schematic profile of the forest, as shown in the following example.

    library(fractalforest)
    library(forestmangr)
    library(dplyr)

    data("exfm20")

    dados <- exfm20 %>%
      filter(!dead)

    fracflor <- build_forest_profile(data = dados,
                                     height = Htot,
                                     diameter = dbh,
                                     label = scientific.name,
                                     leaf_size = 50,
                                     tree_model = 2,
                                     sample = TRUE,
                                     n_trees = 30,
    )

    plot_forest_profile(fracflor,
                        label = scientific.name)

The `build_forest_profile` function creates a fractal tree database from a dataset where each row represents a tree. When the argument `sample = TRUE`, the trees to be plotted are randomly selected from the dataset, and the number of trees is defined by the `n_trees` argument.
The `plot_forest_profile` function uses the `ggplot2` package to plot the fractal forest based on the dataset created by the `build_forest_profile` function.

Some parameters make it possible to customize the visualization of fractal trees, including adding colors, and diameters from the base to the top of the trees.

    fracflor_colors <- fracflor %>%
      select(tree_id) %>%
      distinct() %>%
      rowwise() %>%
      mutate(cor_folhas = sample(c('darkgreen','darkolivegreen','darkolivegreen4','darkseagreen4'), 1),
             cor_ramos = sample(c('firebrick4','brown4','chocolate4','bisque4'),1))

    fracflor2 <- fracflor %>%
      left_join(fracflor_colors, by = 'tree_id')

    plot_forest_profile(fracflor2,
                        d_col = diameter,
                        branch_color = cor_ramos,
                        leaf_color = cor_folhas,
                        label = scientific.name)

There are several ways to introduce randomness when constructing the fractal trees and fractal forests. The `build_forest_profile` function includes arguments that control the variability of branch angles, lengths, and even the distance between trees. The parameters are detailed in the function documentation.

    fracflor <- build_forest_profile(data = dados,
                                     height = Htot,
                                     diameter = dbh,
                                     label = scientific.name,
                                     leaf_size = 50,
                                     randomness = T,
                                     angle_cv = .1,
                                     length_cv = .3,
                                     dist_cv = .2,
                                     tree_model = 6,
                                     sample = TRUE,
                                     n_trees = 30,
    )
    #> Building tree 1 of 30
    Building tree 2 of 30
    Building tree 3 of 30
    Building tree 4
    #> of 30
    Building tree 5 of 30
    Building tree 6 of 30
    Building tree 7 of 30
    Building
    #> tree 8 of 30
    Building tree 9 of 30
    Building tree 10 of 30
    Building tree 11 of
    #> 30
    Building tree 12 of 30
    Building tree 13 of 30
    Building tree 14 of 30
    Building
    #> tree 15 of 30
    Building tree 16 of 30
    Building tree 17 of 30
    Building tree 18 of
    #> 30
    Building tree 19 of 30
    Building tree 20 of 30
    Building tree 21 of 30
    Building
    #> tree 22 of 30
    Building tree 23 of 30
    Building tree 24 of 30
    Building tree 25 of
    #> 30
    Building tree 26 of 30
    Building tree 27 of 30
    Building tree 28 of 30
    Building
    #> tree 29 of 30
    Building tree 30 of 30

    fracflor2 <- fracflor %>%
      left_join(fracflor_colors, by = 'tree_id')

    plot_forest_profile(fracflor2,
                        d_col = diameter,
                        branch_color = cor_ramos,
                        leaf_color = cor_folhas,
                        label = scientific.name)