[This article was first published on Having Fun and Creating Value With the R Language on Lucid Manager, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A fractal is a geometric figure in which the same pattern repeats infinitely at an ever-smaller scale. One of the most simple examples is the H-Fractal. You construct a H-Fractal by starting with the line segments that resemble the capital letter H. You then iteratively place smaller H’s centred at the top and bottom of each free line segment. This article shows how to generate H-Fractals with the R language.

The H-Fractal or H-Tree is a self-similar fractal with ever-smaller line segments. This fractal is a Paeno Curve, which means that it eventually will fill a whole area and becomes a 2-dimensional object. The H-Fractal is a canopy fractal, a type of graph where a line segment is split into smaller lines at the end. Fractal canopies are very useful in creating models of plants.

The H-Fractal principle has practical applications in electronics. For example, microstrip antennae are printed on circuit boards and resemble the tree structure in this fractal. In addition, the splitting of fractal canopies is similar to the structure of bronchial tubes in the human body. A US Patent from 2006 lists a H-Fractal as the geometry for a microwave antenna.

## Method

The algorithm starts with a horizontal line on the interval $[-1,1]$. Each iteration adds new shortened lines to the ends of the existing lines. The first iteration results in a shape that resembles the letter H. After that, the direction of the lines switches at each iteration from horizontal to vertical.

If the number of iterations of the fractal is $p$, then each iteration adds $2^p$ lines to the tree, and the total number of lines in the fractal is $2^{p+1} + 1$.

The new lines are shortened by a factor, which is set at $\frac{1}{\sqrt{2}}$. If you set it any higher, the lines will overlap—a lower value results in a more sparse design.

## Generate H-Fractals with ggplot

The code below starts with setting the initial variables and defines the first line in a data frame. The data frame holds the start and end coordinates and the iteration number.

  library(ggplot2)
## H-Fractal Parameters
p <- 8 ## Number of iterations
a <- 1 / sqrt(2) ## Shortening parameter

## Define horizontal starting line
h_fractal <- data.frame(x1 = -1,
y1 = 0,
x2 = 1,
y2 = 0,
m = 0)

The loop defines the iterations and adds a new row to the data frame for each line. First, the direction flips by setting the dx and dy values to either 0 or 1. The second part defines the row numbers for the previously defined lines and the new lines. The code then calculates the coordinates for the new line segments.

  # Generate coordinates (each line on a row)
for (m in 1:p) {
## Flip direction
dx <- (m - 1) %% 2
dy <- m %% 2
# Previous and current line numbers
i <- (2^(m - 1)):(2^m - 1)
j <- (2^m):(2^(m + 1) - 1)
## Calculate coordinates
h_fractal[j, "x1"] <- c(h_fractal[i, "x1"] - dx * a^m, h_fractal[i, "x2"] - dx * a^m)
h_fractal[j, "x2"] <- c(h_fractal[i, "x1"] + dx * a^m, h_fractal[i, "x2"] + dx * a^m)
h_fractal[j, "y1"] <- c(h_fractal[i, "y1"] - dy * a^m, h_fractal[i, "y2"] - dy * a^m)
h_fractal[j, "y2"] <- c(h_fractal[i, "y1"] + dy * a^m, h_fractal[i, "y2"] + dy * a^m)
## Set line property
h_fractal[j, "m"] <- m
}

Lastly, The fractal is visualised with ggplot and the line segment geometry. The colour of the line slowly fades by setting it to the iteration number.

    ## Visualise
ggplot(h_fractal) +
aes(x = x1, y = y1, xend = x2, yend = y2,
col = m) +
geom_segment(size = .5) +
scale_size(guide = NULL) +
scale_colour_gradientn(colours = gray.colors(p), guide = NULL) +
theme_void()

You can find the code for this series of articles on my GitHub page.

## Variations on the Theme

Besides changing the number of iterations and the shortening factor, you can also play with line thickness, colours, and the lines’ direction. This Shiny application lets you play with all variables in this algorithm to generate