<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/plants/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://fronkonstin.com/2017/07/18/plants/"> R – Fronkonstin</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div class="twitter-share"><a rel="nofollow" href="https://twitter.com/intent/tweet?url=https%3A%2F%2Ffronkonstin.com%2F2017%2F07%2F18%2Fplants%2F&via=aschinchon" class="twitter-share-button" target="_blank">Tweet</a></div>
<blockquote><p>Blue dragonflies dart to and fro<br>
I tie my life to your balloon and let it go<br>
(Warm Foothills, Alt-J)</p>
</blockquote>
<p>In my <a rel="nofollow" href="https://fronkonstin.com/2017/06/26/a-shiny-app-to-draw-curves-based-on-l-systems/" target="_blank">last post</a> I did some drawings based on L-Systems. These drawings are done sequentially. At any step, the state of the drawing can be described by the position (coordinates) and the orientation of the <em>pencil</em>. In that case I only used two kind of operators: <em>drawing</em> a straight line and <em>turning</em> a constant angle. Today I used two more symbols to do <em>stack operations</em>:</p>
<ul>
<li><strong>“[“</strong> Push the current state (position and orientation) of the <em>pencil</em> onto a pushdown<br>
operations stack</li>
<li><strong>“]”</strong> Pop a state from the stack and make it the current state of the <em>pencil</em> (no line is drawn)</li>
</ul>
<p>These operators allow to return to a previous state to continue drawing from there. Using them you can draw plants like these:</p>
<p><img alt="" src="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/07/plant1C.png?fit=604%2C450&ssl=1" class="wp-image-4191" data-recalc-dims="1"></p>
<p><img alt="" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/07/plant2C.png?fit=604%2C450&ssl=1" class="wp-image-4191" data-recalc-dims="1"></p>
<p><img alt="" src="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/07/plant3C.png?fit=604%2C450&ssl=1" class="wp-image-4191" data-recalc-dims="1"></p>
<p><img alt="" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/07/plant4C.png?fit=604%2C450&ssl=1" class="wp-image-4191" data-recalc-dims="1"></p>
<p><img alt="" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/07/plant5C.png?fit=604%2C450&ssl=1" class="wp-image-4191" data-recalc-dims="1"></p>
<p><img alt="" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/07/plant6C.png?fit=604%2C450&ssl=1" class="wp-image-4191" data-recalc-dims="1"></p>
<p>Each image corresponds to a different <strong>axiom</strong>, <strong>rules</strong>, <strong>angle</strong> and <strong>depth</strong>. I described these terms in my previous post. If you want to reproduce them you can find the code below (each image corresponds to a different set of axiom, rules, angle and depth parameters). Change colors, add noise to angles, try your own plants … I am sure you will find nice images:</p>
<pre>

library(gsubfn)
library(stringr)
library(dplyr)
library(ggplot2)

#Plant 1
axiom=&quot;F&quot;
rules=list(&quot;F&quot;=&quot;FF-[-F+F+F]+[+F-F-F]&quot;)
angle=22.5
depth=4

#Plant 2
axiom=&quot;X&quot;
rules=list(&quot;X&quot;=&quot;F[+X][-X]FX&quot;, &quot;F&quot;=&quot;FF&quot;)
angle=25.7
depth=7

#Plant 3
axiom=&quot;X&quot;
rules=list(&quot;X&quot;=&quot;F[+X]F[-X]+X&quot;, &quot;F&quot;=&quot;FF&quot;)
angle=20
depth=7

#Plant 4
axiom=&quot;X&quot;
rules=list(&quot;X&quot;=&quot;F-[[X]+X]+F[+FX]-X&quot;, &quot;F&quot;=&quot;FF&quot;)
angle=22.5
depth=5

#Plant 5
axiom=&quot;F&quot;
rules=list(&quot;F&quot;=&quot;F[+F]F[-F]F&quot;)
angle=25.7
depth=5

#Plant 6
axiom=&quot;F&quot;
rules=list(&quot;F&quot;=&quot;F[+F]F[-F][F]&quot;)
angle=20
depth=5


for (i in 1:depth) axiom=gsubfn(&quot;.&quot;, rules, axiom)

actions=str_extract_all(axiom, &quot;\\d*\\+|\\d*\\-|F|L|R|\\[|\\]|\\|&quot;) %>% unlist

status=data.frame(x=numeric(0), y=numeric(0), alfa=numeric(0))
points=data.frame(x1 = 0, y1 = 0, x2 = NA, y2 = NA, alfa=90, depth=1)


for (action in actions) 
{
  if (action==&quot;F&quot;)
  {
    x=points[1, &quot;x1&quot;]+cos(points[1, &quot;alfa&quot;]*(pi/180))
    y=points[1, &quot;y1&quot;]+sin(points[1, &quot;alfa&quot;]*(pi/180))
    points[1,&quot;x2&quot;]=x
    points[1,&quot;y2&quot;]=y
    data.frame(x1 = x, y1 = y, x2 = NA, y2 = NA, 
               alfa=points[1, &quot;alfa&quot;],
               depth=points[1,&quot;depth&quot;]) %>% rbind(points)->points
  }
  if (action %in% c(&quot;+&quot;, &quot;-&quot;)){
    alfa=points[1, &quot;alfa&quot;]
    points[1, &quot;alfa&quot;]=eval(parse(text=paste0(&quot;alfa&quot;,action, angle)))
  }
  if(action==&quot;[&quot;){ 
    data.frame(x=points[1, &quot;x1&quot;], y=points[1, &quot;y1&quot;], alfa=points[1, &quot;alfa&quot;]) %>% 
      rbind(status) -> status
    points[1, &quot;depth&quot;]=points[1, &quot;depth&quot;]+1
  }
  
  if(action==&quot;]&quot;){ 
    depth=points[1, &quot;depth&quot;]
    points[-1,]->points
    data.frame(x1=status[1, &quot;x&quot;], y1=status[1, &quot;y&quot;], x2=NA, y2=NA, 
               alfa=status[1, &quot;alfa&quot;],
               depth=depth-1) %>% 
      rbind(points) -> points
    status[-1,]->status
  }
}

ggplot() + 
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2), 
               lineend = &quot;round&quot;, 
               colour=&quot;white&quot;,
               data=na.omit(points)) + 
  coord_fixed(ratio = 1) +
  theme(legend.position=&quot;none&quot;,
        panel.background = element_rect(fill=&quot;black&quot;),
        panel.grid=element_blank(),
        axis.ticks=element_blank(),
        axis.title=element_blank(),
        axis.text=element_blank())
</pre>
	<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://fronkonstin.com/2017/07/18/plants/"> R – Fronkonstin</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/R04flzGVb5U" height="1"></a></div>