<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/phyllotaxis-sprial-and-prime-numbers-experiment/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://chichacha.netlify.com/2018/10/28/phyllotaxis-sprial-and-prime-numbers-experiment/"> R on Chi's Impe[r]fect Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I recently tried out <a rel="nofollow" href="https://www.datacamp.com/?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">Data Camp’s</a> project called <a rel="nofollow" href="https://www.datacamp.com/projects/62?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">“Phyllotaxis: Draw flowers using mathematics”</a>. Now I’m hooked on drawing spirals using <a rel="nofollow" href="https://en.wikipedia.org/wiki/Golden_angle" target="_blank">golden angle</a>. Also the mathematical art on instructor’s blog, <a rel="nofollow" href="https://fronkonstin.com/category/drawings/" target="_blank">Fronkostin</a> is just amazing!</p>
<p>Separately on twitter, I’ve gotten message on art with prime number, and that got me thinking to experiment with prime number &amp; phyllotaxis flowers. I couldn’t figure out how I’d generate prime numbers in R, but I came across site where you can <a rel="nofollow" href="http://www.naturalnumbers.org/primes.htm" target="_blank">download prime numbers</a>.</p>
<pre><code>library(tidyverse)
library(patchwork)

## Read first 10000 digits of prime number!
prime &lt;- read_csv(file=&quot;http://www.naturalnumbers.org/P-10000.txt&quot;, col_names=F)
names(prime) &lt;- c(&quot;nth_prime&quot;,&quot;prime&quot;,&quot;int&quot;)  ## int = interval from previous prime number


## Function to Draw Frlower
my_flower &lt;- function(points=5000,num_colour=9,col_option=&quot;magma&quot;,angle=pi*(3-sqrt(5)),...){
  flower &lt;- tibble(
    n = c(1:points), ## change number here to use different # of points
    r = sqrt(n),
    is_prime = n %in% prime$prime, #logical
    colour = n%%num_colour, ## 2,3,6,12,18, seems to bring out the sprial pattern
    x = r * cos(angle*n),
    y = r * sin(angle*n)
  )
  
  prime.cnt &lt;- flower %>% filter(is_prime) %>% count()
  angle_deg &lt;- if(angle==pi*(3-sqrt(5))) {&quot;golden angle!(137.51 degree | 2.4 radian)&quot;} else {paste(round(angle*180/pi,2),&quot;degree | &quot;,round(angle,2),&quot;radian&quot;)}
  
  ## Drawing Flower (but not using Prime Number)
  flower_plot &lt;-flower %>%
    filter(!is_prime) %>%
    ggplot(aes(x=x, y=y, colour=colour)) +
    geom_point() +
    geom_path(size=0.01) +
    scale_colour_viridis_c(end=0.8, guide=&quot;none&quot;, option=col_option) +
    coord_fixed() +
    theme_void(base_family=&quot;Roboto Condensed&quot;) +
    labs(caption=paste(num_colour, &quot;colours used to plot&quot;, points-prime.cnt,&quot;dots.\nAngle Used: &quot;, angle_deg), 
         subtitle=&quot;Flower Nibbled by Prime Number Bug&quot;)
  
  ## Drawing Flower (only using Prime Number)
  flower_prime &lt;-flower %>%
    filter(is_prime) %>%
    ggplot(aes(x=x, y=y, colour=colour)) +
    geom_point() +
    scale_colour_viridis_c(end=0.8, guide=&quot;none&quot;, option=col_option) +
    coord_fixed() +
    theme_void(base_family=&quot;Roboto Condensed&quot;) +
    labs(caption=paste(&quot;Numbers between 1 and &quot;,points, &quot;have&quot;, prime.cnt,&quot; Prime Numbers\n&quot;),
         subtitle=&quot;Flower made up by Prime Numbers Only&quot;)
  
  #You need to Print
  flower_plot + flower_prime
  
}</code></pre>
<div>
<h2>Experimenting with Different Variables</h2>
<p>I’ve wrote function to draw flower as above, so I can now experiment by changing below.</p>
<ul>
<li>points = Number of points e.g. Up to what number should we use to draw flower? (up to 104729)</li>
<li>num_colour = Number of colours to use. When golden angle is used, seems like multiple of 6 makes colours line up?</li>
<li>col_option = I can use magma,viridis, plasma, inferno or cividis here</li>
<li>angle = Angle to use for drawing spirals. Default is set to golden angle pi*(3-sqrt(5))</li>
</ul>
<pre><code>my_flower()  ## drawing with all default values e.g. 5000 points, 9 colours, angle= golden angle</code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-10-28-phyllotaxis-sprial-and-prime-numbers-experiment_files/figure-html/Testing_Function-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>my_flower(angle = 2*pi-(pi*(3-sqrt(5))))  ## changing angle 2pi - golden angle so it looks like it's the reverse of the above.</code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-10-28-phyllotaxis-sprial-and-prime-numbers-experiment_files/figure-html/Testing_Function-2.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>my_flower(angle=pi/sqrt(7))  ##  Just testing out different angles</code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-10-28-phyllotaxis-sprial-and-prime-numbers-experiment_files/figure-html/Testing_Function-3.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>my_flower(angle=pi/sqrt(pi), num_col=6*3) ## Using 18 different colours, but hard to distinguish the colour...  </code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-10-28-phyllotaxis-sprial-and-prime-numbers-experiment_files/figure-html/Testing_Function-4.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>my_flower(angle=exp(1), num_col=10) ## exp(1) = Euler's Number as angle. </code></pre>
<p><img src="https://i2.wp.com/chichacha.netlify.com/post/2018-10-28-phyllotaxis-sprial-and-prime-numbers-experiment_files/figure-html/Testing_Function-5.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>my_flower(angle = 0.3, num_col=3) ## All the black arm goes away when you only use prime number</code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-10-28-phyllotaxis-sprial-and-prime-numbers-experiment_files/figure-html/Testing_Function-6.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>my_flower(num_col=6*3, col_option=&quot;plasma&quot;) ## plasma colour palette! </code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-10-28-phyllotaxis-sprial-and-prime-numbers-experiment_files/figure-html/Testing_Function-7.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>my_flower(points=10000,num_col=18, col_option=&quot;viridis&quot;)  ## little over crowded...</code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-10-28-phyllotaxis-sprial-and-prime-numbers-experiment_files/figure-html/Testing_Function-8.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>I could play with different angles all day long! So intriguing…</p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://chichacha.netlify.com/2018/10/28/phyllotaxis-sprial-and-prime-numbers-experiment/"> R on Chi's Impe[r]fect Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/1a1nj9gJA2c" height="1"></a></div>