<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/brownian-motion-gif-with-r-and-imagemagick/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://allthiswasfield.blogspot.com/2017/12/p-margin-bottom-0.html"> long time ago...</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>

<p></p>

<p></p>
<div lang="en-US">Hi there!</div>
<div lang="en-US"></div>
<div lang="en-US">Last Monday we celebrated a “Scientific Marathon” at Royal Botanic Garden in Madrid, a kind of mini-conference to talk about our research. I was talking about the relation between fungal spore size and environmental variables such as temperature and precipitation. To make my presentation more friendly, I created a GIF to explain the Brownian Motion model. In evolutionary biology, we can use this model to simulate the random variation of a continuous trait through time. Under this model, we can notice how closer species tend to maintain closer trait values due to shared evolutionary history. You have a lot of information about Brownian Motion models in evolutionary biology <a rel="nofollow" href="https://lukejharmon.github.io/pcm/chapter3_bmintro/" target="_blank">everywhere</a>!</div>
<div lang="en-US">Here I will show you how I built a GIF to explain Brownian Motion in my talk using R and ImageMagick.</div>
<div lang="en-US"></div>
<div lang="en-US"></div>
<pre><code> # First, we simulate continuous trait evolution by adding in each iteration  <br> # a random number from a normal distribution with mean equal to 0 and standard  <br> # deviation equal to 1. We simulate a total of 4 processes, to obtain at first  <br> # two species and a specieation event at the middle of the simulation, obtaining  <br> # a total of 3 species at the end.  <br> df1&lt;- data.frame(0,0)  <br> names(df1)&lt;- c(&quot;Y&quot;,&quot;X&quot;)  <br> y&lt;-0  <br> for (g in 1:750){  <br> df1[g,2] &lt;- g  <br> df1[g,1] &lt;- y  <br> y &lt;- y + rnorm(1,0,1)  <br> }  <br> #plot(df1$X,df1$Y, ylim=c(-100,100), xlim=c(0,1500), cex=0)  <br> #lines(df1$X,df1$Y, col=&quot;red&quot;)  <br> df2&lt;- data.frame(0,0)  <br> names(df2)&lt;- c(&quot;Y&quot;,&quot;X&quot;)  <br> y&lt;-0  <br> for (g in 1:1500){  <br>  df2[g,2] &lt;- g  <br>  df2[g,1] &lt;- y  <br>  y &lt;- y + rnorm(1,0,1)  <br> }  <br> #lines(df2$X,df2$Y, col=&quot;blue&quot;)  <br> df3&lt;- data.frame(750,df1[750,1])  <br> names(df3)&lt;- c(&quot;Y&quot;,&quot;X&quot;)  <br> y&lt;-df1[750,1]  <br> for (g in 750:1500){  <br>  df3[g-749,2] &lt;- g  <br>  df3[g-749,1] &lt;- y  <br>  y &lt;- y + rnorm(1,0,1)  <br> }  <br> #lines(df3$X,df3$Y, col=&quot;green&quot;)  <br> df4&lt;- data.frame(750,df1[750,1])  <br> names(df4)&lt;- c(&quot;Y&quot;,&quot;X&quot;)  <br> y&lt;-df1[750,1]  <br> for (g in 750:1500){  <br>  df4[g-749,2] &lt;- g  <br>  df4[g-749,1] &lt;- y  <br>  y &lt;- y + rnorm(1,0,1)  <br> }  <br> #lines(df4$X,df4$Y, col=&quot;orange&quot;)  <br></code></pre>
<p></p>
<div><a rel="nofollow" href="https://i2.wp.com/3.bp.blogspot.com/-cnb_UJUWwCc/WiwEekDcxnI/AAAAAAAAANI/4e9BO5YespYtoetLI1wQ_315ayQd4Kp1wCEwYBhgL/s1600/bm1498.png?ssl=1" target="_blank"><img border="0" data-original-height="570" src="https://i1.wp.com/3.bp.blogspot.com/-cnb_UJUWwCc/WiwEekDcxnI/AAAAAAAAANI/4e9BO5YespYtoetLI1wQ_315ayQd4Kp1wCEwYBhgL/s400/bm1498.png?resize=450%2C570&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<pre><code> # Now, we have to plot each simmulation lapse and store them in our computer.  <br> # I added some code to make lighter the gif (plotting just odd generations) and   <br> # to add a label at the speciation time. Note that, since Brownan Model is a   <br> # stocasthic process, my simulation will be different from yours.  <br> # You should adjust labels or repeat the simulation process if you don't   <br> # like the shape of your plot.  <br> parp&lt;-rep(0:1, times=7, each= 15)  <br> parp&lt;- c(parp, rep(0, 600))  <br> for (q in 1:750){  <br>  if ( q %% 2 == 1) {  <br>  id &lt;- sprintf(&quot;%04d&quot;, q+749)  <br>  png(paste(&quot;bm&quot;,id,&quot;.png&quot;, sep=&quot;&quot;), width=900, height=570, units=&quot;px&quot;,   <br>    pointsize=18)  <br>  par(omd = c(.05, 1, .05, 1))  <br>  plot(df1$X,df1$Y, ylim=c(-70,70), xlim=c(0,1500), cex=0,   <br>     main=paste(&quot;Brownian motion model \n generation=&quot;, 749 + q) ,   <br>     xlab=&quot;generations&quot;, ylab=&quot;trait value&quot;, font.lab=2, cex.lab=1.5 )  <br> lines(df1$X,df1$Y, col=&quot;red&quot;, lwd=4)  <br> lines(df2$X[1:(q+749)],df2$Y[1:(q+749)], col=&quot;blue&quot;, lwd=4)  <br> lines(df3$X[1:q],df3$Y[1:q], col=&quot;green&quot;, lwd=4)  <br> lines(df4$X[1:q],df4$Y[1:q], col=&quot;orange&quot;, lwd=4)  <br> if (parp[q]==0)  <br> text(750, 65,labels=&quot;speciation event&quot;, cex= 1.5, col=&quot;black&quot;, font=2)  <br> if (parp[q]==0)  <br> arrows(750, 60, 750, 35, length = 0.20, angle = 30, lwd = 3)  <br> dev.off()  <br> }  <br> }  <br></code></pre>
<p> Now, you just have to use ImageMagick to put all the PNG files together in a GIF using a command like this in a terminal:</p>
<p></p>
<pre><code> convert -delay 10 *.png bm.gif  <br></code></pre>
<p>Et voilà!</p>
<p></p>
<div><a rel="nofollow" href="https://i0.wp.com/1.bp.blogspot.com/-y_ys5AJz5SQ/WiwF_nq8JKI/AAAAAAAAANY/hS7IIviDbPwdSlTmOJEbEtfxdFB7MJ06ACEwYBhgL/s1600/bm_fast2.gif?ssl=1" target="_blank"><img border="0" data-original-height="570" src="https://i0.wp.com/1.bp.blogspot.com/-y_ys5AJz5SQ/WiwF_nq8JKI/AAAAAAAAANY/hS7IIviDbPwdSlTmOJEbEtfxdFB7MJ06ACEwYBhgL/s400/bm_fast2.gif?resize=450%2C570&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://allthiswasfield.blogspot.com/2017/12/p-margin-bottom-0.html"> long time ago...</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/gkEHWxecWwg" height="1"></a></div>