knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "60%" )
The goal of fractaltree is to plot trees as fractals. Using the shape of a single "leaf", every subsequent growht takes the same shape.
library(fractaltree) library(magrittr) library(dplyr) leaf <- local({ matrix( c(0, 0, 0, 1, 0, 0.45, 0.2, 1, 0, 0.45, -0.2, 1), byrow = TRUE, ncol = 4 ) }) tree <- fractal_tree(leaf, depth = 3, growth_fraction = 1) tree %>% filter(i <= 1) %>% bind_rows( tree %>% filter(i <= 2) %>% translate(c(1.5, 0)) ) %>% bind_rows( tree %>% filter(i <= 4) %>% translate(c(4, 0)) ) %>% plot_tree()
devtools::install_github("andrie/fractaltree")
You can use the depth
argument to change the number of growth cycles, and the shrinkage_factor
to control how much growth with every generation. If the growth is high, the resulting image may resemble a shrub, and if the growth is low, the resulting image may resemble a mature tree.
fractal_tree(leaf, depth = 5, growth_fraction = 1) %>% plot_tree() fractal_tree(leaf, depth = 5, growth_fraction = 0.75) %>% plot_tree() fractal_tree(leaf, depth = 5, growth_fraction = 0.5) %>% plot_tree()
You can make interesting tree shapes if the leaf shape is asymmetric:
leaf <- local({ matrix( c(0, 0, 0, 1, 0, 0.45, 0.12, 0.80, 0, 0.85, -0.04, 0.95), byrow = TRUE, ncol = 4 ) }) leaf %>% fractal_tree(depth = 1) %>% plot_tree() leaf %>% fractal_tree(depth = 7, growth_fraction = 0.6) %>% plot_tree()
You can use the functions translate()
, rotate()
and shrink()
to perform spatial transformation on a tree.
In addition you can use the kaleidoscope()
function to turn a tree into a symmetric diagram:
leaf %>% fractal_tree(depth = 3, growth_fraction = 0.8) %>% kaleidoscope() %>% plot_tree(colors = c( "red", "blue"))
By first translating a tree in 2-dimensional space, using the translate()
function, you can create very interesting kaleidoscope()
images:
leaf %>% fractal_tree(depth = 3, growth_fraction = 0.8) %>% kaleidoscope() %>% translate(c(0, 5)) %>% kaleidoscope() %>% plot_tree(colors = c( "red", "blue"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.