Introduction:

Most of you will have heard about the number called the golden ratio. It appears, for example, in the book/film The da Vinci Code and in many articles, books, and school projects, which aim to show how mathematics is important in the real world. It has been described by many authors (including the writer of the da Vinci Code) as the basis of all of the beautiful patterns in nature and it is sometimes referred to as the divine proportion. It is claimed that much of art and architecture contains features in proportions given by the golden ratio. Since the time of the ancient Greeks artists, designer, thinkers, scientists, and engineers,have been fascinated by the golden ratio also denoted by as \(\phi\) . The golden ratio has over the centuries even been considered as the perfect illustration of beauty.

👉 To get more insights about golden ratio click here.

Here we’re going to construct Fibonacci Spiral.

Required Libraries:

#install.packages(c("tidyverse","plotrix"))
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(plotrix)

Transforming into Polar Coordinates:

In polar coordinates, this special instance of a logarithmic spiral’s functional representation can be simplified to r(t) = e(0.0635*t) For every quarter turn, the corresponding point on the spiral is a factor of phi further from the origin (r is this distance), with phi the golden ratio - the same one obtained from dividing any 2 sufficiently big successive numbers on a Fibonacci sequence, which is how the golden ratio, the golden spiral and Fibonacci sequences are linked concepts.

polar_golden_spiral <- function(theta) exp(0.30635*theta)

Let’s do 2 full circles. First one create a sequence of angle values theta. Since 2*\(\pi\) is the equivalent of a circle in polar coordinates, we need to have distances from origin for values between 0 and 4*\(\pi\).

seq_theta <- seq(0,4*pi,by=0.05)
dist_from_origin <- sapply(seq_theta,polar_golden_spiral)

Plotting the function using coord_polar in ggplot2 does not work as intended. Unexpectedly, the x axis keeps extending instead of circling back once a full circle is reached. Turns out coord_polar might not really be intended to plot elements in polar vector format.

ggplot(data.frame(x = seq_theta, y = dist_from_origin), aes(x,y)) +
    geom_point() +
    coord_polar(theta="x")

With that established and the original objective of the exercise achieved, it still would be nice to be able to accomplish this using ggplot2. To do so, the created sequence above needs to be converted to cartesian coordinates. The rectangular function equivalent of the golden spiral function r(t) defined above is a(t) = (r(t) cos(t), r(t) sin(t)) It’s not too hard to come up with a hack to convert one to the other.

plotrix::radial.plot(dist_from_origin, seq_theta,rp.type="s", point.col = "blue")

cartesian_golden_spiral <- function(theta) {
    a <- polar_golden_spiral(theta)*cos(theta)
    b <- polar_golden_spiral(theta)*sin(theta)
    c(a,b)
}

Applying that function to the same series of angles from above and stitching the resulting coordinates in a data frame. Note I’m enclosing the first expression in brackets, which prints it immediately, which is useful when the script is run interactively.

(serie <- sapply(seq_theta,cartesian_golden_spiral))
##      [,1]       [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
## [1,]    1 1.01416638 1.0259578 1.0352677 1.0419930 1.0460346 1.0472978
## [2,]    0 0.05075062 0.1029391 0.1564654 0.2112224 0.2670965 0.3239672
##           [,8]      [,9]     [,10]     [,11]     [,12]     [,13]     [,14]
## [1,] 1.0456927 1.0411345 1.0335440 1.0228479 1.0089793 0.9918780 0.9714910
## [2,] 0.3817076 0.4401846 0.4992586 0.5587843 0.6186104 0.6785802 0.7385317
##          [,15]     [,16]     [,17]     [,18]     [,19]     [,20]     [,21]
## [1,] 0.9477725 0.9206849 0.8901986 0.8562923 0.8189539 0.7781803 0.7339778
## [2,] 0.7982978 0.8577068 0.9165828 0.9747455 1.0320115 1.0881938 1.1431027
##          [,22]     [,23]     [,24]     [,25]     [,26]     [,27]     [,28]
## [1,] 0.6863625 0.6353603 0.5810075 0.5233508 0.4624475 0.3983656 0.3311842
## [2,] 1.1965462 1.2483302 1.2982595 1.3461376 1.3917679 1.4349537 1.4754991
##          [,29]     [,30]     [,31]      [,32]       [,33]      [,34]      [,35]
## [1,] 0.2609934 0.1878944 0.1119997 0.03343311 -0.04767048 -0.1311648 -0.2168924
## [2,] 1.5132094 1.5478917 1.5793555 1.60741330  1.63188129  1.6525798  1.6693342
##           [,36]      [,37]      [,38]      [,39]      [,40]      [,41]
## [1,] -0.3046847 -0.3943623 -0.4857347 -0.5786012 -0.6727502 -0.7679604
## [2,]  1.6819752  1.6903398  1.6942717  1.6936220  1.6882502  1.6780241
##           [,42]      [,43]     [,44]     [,45]     [,46]     [,47]     [,48]
## [1,] -0.8640004 -0.9606293 -1.057597 -1.154646 -1.251507 -1.347908 -1.443565
## [2,]  1.6628212  1.6425288  1.617045  1.586279  1.550152  1.508597  1.461561
##          [,49]     [,50]     [,51]     [,52]     [,53]     [,54]     [,55]
## [1,] -1.538190 -1.631489 -1.723160 -1.812899 -1.900397 -1.985341 -2.067415
## [2,]  1.409004  1.350901  1.287239  1.218023  1.143272  1.063022  0.977324
##           [,56]      [,57]      [,58]      [,59]      [,60]      [,61]
## [1,] -2.1463023 -2.2216852 -2.2932451 -2.3606644 -2.4236271 -2.4818198
## [2,]  0.8862466  0.7898754  0.6883131  0.5816804  0.4701156  0.3537748
##           [,62]      [,63]       [,64]      [,65]      [,66]      [,67]
## [1,] -2.5349325 -2.5826597 -2.62470145 -2.6607641 -2.6905614 -2.7138159
## [2,]  0.2328327  0.1074817 -0.02206729 -0.1555851 -0.2928246 -0.4335206
##           [,68]      [,69]      [,70]     [,71]     [,72]     [,73]     [,74]
## [1,] -2.7302594 -2.7396345 -2.7416950 -2.736208 -2.722954 -2.701727 -2.672339
## [2,] -0.5773898 -0.7241317 -0.8734282 -1.024944 -1.178328 -1.333212 -1.489214
##          [,75]     [,76]     [,77]     [,78]     [,79]     [,80]     [,81]
## [1,] -2.634618 -2.588409 -2.533576 -2.470004 -2.397596 -2.316279 -2.226002
## [2,] -1.645933 -1.802959 -1.959863 -2.116208 -2.271541 -2.425400 -2.577312
##          [,82]     [,83]     [,84]     [,85]     [,86]     [,87]     [,88]
## [1,] -2.126736 -2.018478 -1.901247 -1.775092 -1.640084 -1.496322 -1.343934
## [2,] -2.726794 -2.873356 -3.016500 -3.155723 -3.290515 -3.420365 -3.544758
##          [,89]     [,90]      [,91]      [,92]     [,93]      [,94]       [,95]
## [1,] -1.183074 -1.013925 -0.8366994 -0.6516373 -0.459009 -0.2591138 -0.05228068
## [2,] -3.663180 -3.775116 -3.8800530 -3.9774824 -4.066900 -4.1478082 -4.21971780
##          [,96]      [,97]      [,98]     [,99]    [,100]    [,101]    [,102]
## [1,]  0.161132  0.3807364  0.6061154  0.836823  1.072384  1.312297  1.556029
## [2,] -4.282149 -4.3346342 -4.3767177 -4.407959 -4.427935 -4.436238 -4.432484
##         [,103]    [,104]    [,105]    [,106]    [,107]    [,108]    [,109]
## [1,]  1.803024  2.052696  2.304437  2.557611  2.811561  3.065605  3.319041
## [2,] -4.416307 -4.387365 -4.345343 -4.289949 -4.220922 -4.138029 -4.041068
##         [,110]    [,111]    [,112]    [,113]    [,114]    [,115]    [,116]
## [1,]  3.571147  3.821180  4.068384  4.311982  4.551187  4.785198  5.013204
## [2,] -3.929872 -3.804306 -3.664272 -3.509709 -3.340593 -3.156941 -2.958812
##         [,117]    [,118]   [,119]    [,120]    [,121]    [,122]    [,123]
## [1,]  5.234384  5.447913  5.65296  5.848691  6.034273  6.208875  6.371672
## [2,] -2.746305 -2.519562 -2.27877 -2.024160 -1.756011 -1.474644 -1.180430
##          [,124]     [,125]    [,126]    [,127]    [,128]    [,129]   [,130]
## [1,]  6.5218429  6.6585790  6.781083 6.8885708 6.9802779 7.0554586 7.113390
## [2,] -0.8737863 -0.5551771 -0.225115 0.1158401 0.4670804 0.8279506 1.197749
##        [,131]   [,132]   [,133]   [,134]   [,135]   [,136]   [,137]   [,138]
## [1,] 7.153374 7.174743 7.176857 7.159111 7.120938 7.061807 6.981230 6.878765
## [2,] 1.575725 1.961086 2.352990 2.750553 3.152848 3.558904 3.967712 4.378222
##        [,139]   [,140]   [,141]   [,142]   [,143]   [,144]   [,145]   [,146]
## [1,] 6.754015 6.606633 6.436323 6.242846 6.026019 5.785716 5.521874 5.234494
## [2,] 4.789347 5.199965 5.608921 6.015026 6.417066 6.813797 7.203952 7.586245
##        [,147]   [,148]   [,149]   [,150]   [,151]   [,152]   [,153]    [,154]
## [1,] 4.923641 4.589448 4.232117 3.851921 3.449202 3.024380 2.577944  2.110464
## [2,] 7.959368 8.322001 8.672811 9.010456 9.333589 9.640862 9.930927 10.202445
##         [,155]    [,156]     [,157]      [,158]     [,159]    [,160]    [,161]
## [1,]  1.622581  1.115016  0.5885655  0.04410341 -0.5174188 -1.094973 -1.687454
## [2,] 10.454084 10.684527 10.8924761 11.07665315 11.2358075 11.368719 11.474202
##         [,162]    [,163]    [,164]    [,165]    [,166]    [,167]    [,168]
## [1,] -2.293682 -2.912401 -3.542283 -4.181924 -4.829855 -5.484532 -6.144349
## [2,] 11.551111 11.598342 11.614843 11.599610 11.551699 11.470227 11.354375
##         [,169]    [,170]    [,171]    [,172]    [,173]     [,174]     [,175]
## [1,] -6.807634 -7.472653 -8.137613 -8.800668 -9.459916 -10.113408 -10.759153
## [2,] 11.203396 11.016616 10.793440 10.533355 10.235935   9.900845   9.527842
##          [,176]     [,177]     [,178]     [,179]     [,180]     [,181]
## [1,] -11.395115 -12.019225 -12.629381 -13.223456 -13.799300 -14.354746
## [2,]   9.116784   8.667627   8.180432   7.655371   7.092721   6.492876
##          [,182]     [,183]     [,184]     [,185]     [,186]     [,187]
## [1,] -14.887618 -15.395735 -15.876915 -16.328986 -16.749785 -17.137173
## [2,]   5.856345   5.183752   4.475844   3.733487   2.957671   2.149509
##          [,188]      [,189]      [,190]     [,191]     [,192]     [,193]
## [1,] -17.489033 -17.8032851 -18.0778854 -18.310838 -18.500202 -18.644094
## [2,]   1.310237   0.4412194  -0.4560578  -1.379982  -2.328818  -3.300706
##          [,194]     [,195]    [,196]     [,197]     [,198]     [,199]    [,200]
## [1,] -18.740700 -18.788282 -18.78518 -18.729832 -18.620760 -18.456599 -18.23609
## [2,]  -4.293664  -5.305592  -6.33427  -7.377363  -8.432424  -9.496896 -10.56812
##         [,201]    [,202]    [,203]    [,204]    [,205]    [,206]    [,207]
## [1,] -17.95809 -17.62158 -17.22569 -16.76965 -16.25287 -15.67489 -15.03541
## [2,] -11.64332 -12.71965 -13.79415 -14.86377 -15.92541 -16.97586 -18.01186
##         [,208]    [,209]    [,210]    [,211]    [,212]     [,213]     [,214]
## [1,] -14.33430 -13.57158 -12.74745 -11.86229 -10.91666  -9.911306  -8.847159
## [2,] -19.03008 -20.02714 -20.99962 -21.94405 -22.85693 -23.734759 -24.574000
##          [,215]     [,216]     [,217]     [,218]     [,219]     [,220]
## [1,]  -7.725346  -6.547186  -5.314197  -4.028098  -2.690806  -1.304441
## [2,] -25.371123 -26.122607 -26.824943 -27.474655 -28.068299 -28.602486
##           [,221]     [,222]     [,223]     [,224]     [,225]     [,226]
## [1,]   0.1286735   1.606014   3.124854   4.682268   6.275133   7.900128
## [2,] -29.0738809 -29.479222 -29.815330 -30.079117 -30.267602 -30.377917
##          [,227]    [,228]    [,229]    [,230]    [,231]    [,232]    [,233]
## [1,]   9.553743  11.23228  12.93184  14.64838  16.37764  18.11524  19.85659
## [2,] -30.407326 -30.35323 -30.21318 -29.98489 -29.66626 -29.25534 -28.75043
##         [,234]    [,235]    [,236]    [,237]    [,238]    [,239]    [,240]
## [1,]  21.59699  23.33157  25.05533  26.76316  28.44982  30.10996  31.73815
## [2,] -28.14998 -27.45271 -26.65752 -25.76359 -24.77032 -23.67738 -22.48470
##         [,241]    [,242]    [,243]    [,244]    [,245]    [,246]    [,247]
## [1,]  33.32888  34.87656  36.37556  37.82020  39.20479  40.52360  41.77093
## [2,] -21.19250 -19.80126 -18.31176 -16.72509 -15.04263 -13.26606 -11.39739
##         [,248]    [,249]    [,250]    [,251]     [,252]
## [1,] 42.941100 44.028453 45.027396 45.932405 46.7380447
## [2,] -9.438953 -7.393381 -5.263648 -3.053046 -0.7651989
df <- data.frame(t(serie))

Final Result:

With everything now ready in the right coordinate system, it’s now only a matter of setting some options to make the output look acceptable.

ggplot(df, aes(x=X1,y=X2)) +
    geom_path(color="blue") +
    theme(panel.grid.minor = element_blank(),
      axis.text.x = element_blank(),
      axis.text.y = element_blank()) +
    scale_y_continuous(breaks = seq(-20,20,by=10)) +
    scale_x_continuous(breaks = seq(-20,50,by=10)) +
    coord_fixed() +
    labs(title = "Golden spiral",
     subtitle = "Another view on the Fibonacci sequence",
     caption = "www.linkedin.com/in/abhirup-moitra-7a66731a6
                https://github.com/ABHI20-STAT.",
     x = "",
     y = "")