<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/chaotic-galaxies/"></div></div><div><a data-url="https://www.r-bloggers.com/chaotic-galaxies/" data-counturl="https://www.r-bloggers.com/chaotic-galaxies/" data-count="horizontal" data-text="Chaotic Galaxies" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://aschinchon.wordpress.com/2016/09/06/chaotic-galaxies/"> Ripples</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<blockquote><p>Tell me, which side of the earth does this nose come from? Ha! (ALF)</p>
</blockquote>
<p>Reading about strange attractors I came across with <a target="_blank" href="http://sprott.physics.wisc.edu/sa.htm">this book</a>, where I discovered a way to generate two dimensional <a target="_blank" href="https://en.wikipedia.org/wiki/Chaos_theory">chaotic maps</a>. The generic equation is pretty simple:</p>
<p><img title="x_{n+1}= a_{1}+a_{2}x_{n}+a_{3}x_{n}^{2}+a_{4}x_{n}y_{n}+a_{5}y_{n}+a_{6}y_{n}^{2} " alt="x_{n+1}= a_{1}+a_{2}x_{n}+a_{3}x_{n}^{2}+a_{4}x_{n}y_{n}+a_{5}y_{n}+a_{6}y_{n}^{2} " class="latex" src="https://s0.wp.com/latex.php?latex=x_%7Bn%2B1%7D%3D+a_%7B1%7D%2Ba_%7B2%7Dx_%7Bn%7D%2Ba_%7B3%7Dx_%7Bn%7D%5E%7B2%7D%2Ba_%7B4%7Dx_%7Bn%7Dy_%7Bn%7D%2Ba_%7B5%7Dy_%7Bn%7D%2Ba_%7B6%7Dy_%7Bn%7D%5E%7B2%7D+&bg=ffffff&%23038;fg=141412&%23038;s=0"><br>
<img title="y_{n+1}= a_{7}+a_{8}x_{n}+a_{9}x_{n}^{2}+a_{10}x_{n}y_{n}+a_{11}y_{n}+a_{12}y_{n}^{2} " alt="y_{n+1}= a_{7}+a_{8}x_{n}+a_{9}x_{n}^{2}+a_{10}x_{n}y_{n}+a_{11}y_{n}+a_{12}y_{n}^{2} " class="latex" src="https://s0.wp.com/latex.php?latex=y_%7Bn%2B1%7D%3D+a_%7B7%7D%2Ba_%7B8%7Dx_%7Bn%7D%2Ba_%7B9%7Dx_%7Bn%7D%5E%7B2%7D%2Ba_%7B10%7Dx_%7Bn%7Dy_%7Bn%7D%2Ba_%7B11%7Dy_%7Bn%7D%2Ba_%7B12%7Dy_%7Bn%7D%5E%7B2%7D+&bg=ffffff&%23038;fg=141412&%23038;s=0"></p>
<p>I used it to generate these <em>chaotic galaxies</em>:</p>
<p><a target="_blank" href="https://aschinchon.wordpress.com/2016/09/06/chaotic-galaxies/galaxy4/"><img data-image-description="" data-orig-file="https://aschinchon.files.wordpress.com/2016/08/galaxy41.png" data-orig-size="850,850" data-image-title="galaxy4" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="3265" alt="galaxy4" data-medium-file="https://aschinchon.files.wordpress.com/2016/08/galaxy41.png?w=300" data-large-file="https://aschinchon.files.wordpress.com/2016/08/galaxy41.png?w=604" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i2.wp.com/aschinchon.files.wordpress.com/2016/08/galaxy41.png?resize=150%2C150&ssl=1"></a><br>
<a target="_blank" href="https://aschinchon.wordpress.com/2016/09/06/chaotic-galaxies/galaxy3/"><img data-image-description="" data-orig-file="https://aschinchon.files.wordpress.com/2016/08/galaxy31.png" data-orig-size="850,850" data-image-title="galaxy3" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="3264" alt="galaxy3" data-medium-file="https://aschinchon.files.wordpress.com/2016/08/galaxy31.png?w=300" data-large-file="https://aschinchon.files.wordpress.com/2016/08/galaxy31.png?w=604" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/aschinchon.files.wordpress.com/2016/08/galaxy31.png?resize=150%2C150&ssl=1"></a><br>
<a target="_blank" href="https://aschinchon.wordpress.com/2016/09/06/chaotic-galaxies/galaxy2/"><img data-image-description="" data-orig-file="https://aschinchon.files.wordpress.com/2016/08/galaxy21.png" data-orig-size="850,850" data-image-title="galaxy2" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="3263" alt="galaxy2" data-medium-file="https://aschinchon.files.wordpress.com/2016/08/galaxy21.png?w=300" data-large-file="https://aschinchon.files.wordpress.com/2016/08/galaxy21.png?w=604" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/aschinchon.files.wordpress.com/2016/08/galaxy21.png?resize=150%2C150&ssl=1"></a><br>
<a target="_blank" href="https://aschinchon.wordpress.com/2016/09/06/chaotic-galaxies/galaxy1/"><img data-image-description="" data-orig-file="https://aschinchon.files.wordpress.com/2016/08/galaxy11.png" data-orig-size="850,850" data-image-title="galaxy1" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="3262" alt="galaxy1" data-medium-file="https://aschinchon.files.wordpress.com/2016/08/galaxy11.png?w=300" data-large-file="https://aschinchon.files.wordpress.com/2016/08/galaxy11.png?w=604" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i0.wp.com/aschinchon.files.wordpress.com/2016/08/galaxy11.png?resize=150%2C150&ssl=1"></a></p>
<p>Changing the vector of parameters you can obtain other galaxies. Do you want to try?</p>
<pre>
library(ggplot2)
library(dplyr)
#Generic function
attractor = function(x, y, z)
{
  c(z[1]+z[2]*x+z[3]*x^2+ z[4]*x*y+ z[5]*y+ z[6]*y^2, 
    z[7]+z[8]*x+z[9]*x^2+z[10]*x*y+z[11]*y+z[12]*y^2)
}
#Function to iterate the generic function over the initial point c(0,0)
galaxy= function(iter, z)
{
  df=data.frame(x=0,y=0)
  for (i in 2:iter) df[i,]=attractor(df[i-1, 1], df[i-1, 2], z)
  df %>% rbind(data.frame(x=runif(iter/10, min(df$x), max(df$x)), 
                          y=runif(iter/10, min(df$y), max(df$y))))-> df
  return(df)
}
opt=theme(legend.position=&quot;none&quot;,
          panel.background = element_rect(fill=&quot;#00000c&quot;),
          plot.background = element_rect(fill=&quot;#00000c&quot;),
          panel.grid=element_blank(),
          axis.ticks=element_blank(),
          axis.title=element_blank(),
          axis.text=element_blank(),
          plot.margin=unit(c(-0.1,-0.1,-0.1,-0.1), &quot;cm&quot;))
#First galaxy
z1=c(1.0, -0.1, -0.2,  1.0,  0.3,  0.6,  0.0,  0.2, -0.6, -0.4, -0.6,  0.6)
galaxy1=galaxy(iter=2400, z=z1) %>% ggplot(aes(x,y))+
  geom_point(shape= 8, size=jitter(12, factor=4), color=&quot;#ffff99&quot;, alpha=jitter(.05, factor=2))+
  geom_point(shape=16, size= jitter(4, factor=2), color=&quot;#ffff99&quot;, alpha=jitter(.05, factor=2))+
  geom_point(shape=46, size= 0, color=&quot;#ffff00&quot;)+opt
#Second galaxy
z2=c(-1.1, -1.0,  0.4, -1.2, -0.7,  0.0, -0.7,  0.9,  0.3,  1.1, -0.2,  0.4)
galaxy2=galaxy(iter=2400, z=z2) %>% ggplot(aes(x,y))+
  geom_point(shape= 8, size=jitter(12, factor=4), color=&quot;#ffff99&quot;, alpha=jitter(.05, factor=2))+
  geom_point(shape=16, size= jitter(4, factor=2), color=&quot;#ffff99&quot;, alpha=jitter(.05, factor=2))+
  geom_point(shape=46, size= 0, color=&quot;#ffff00&quot;)+opt
#Third galaxy
z3=c(-0.3,  0.7,  0.7,  0.6,  0.0, -1.1,  0.2, -0.6, -0.1, -0.1,  0.4, -0.7)
galaxy3=galaxy(iter=2400, z=z3) %>% ggplot(aes(x,y))+
  geom_point(shape= 8, size=jitter(12, factor=4), color=&quot;#ffff99&quot;, alpha=jitter(.05, factor=2))+
  geom_point(shape=16, size= jitter(4, factor=2), color=&quot;#ffff99&quot;, alpha=jitter(.05, factor=2))+
  geom_point(shape=46, size= 0, color=&quot;#ffff00&quot;)+opt
#Fourth galaxy
z4=c(-1.2, -0.6, -0.5,  0.1, -0.7,  0.2, -0.9,  0.9,  0.1, -0.3, -0.9,  0.3)
galaxy4=galaxy(iter=2400, z=z4) %>% ggplot(aes(x,y))+
  geom_point(shape= 8, size=jitter(12, factor=4), color=&quot;#ffff99&quot;, alpha=jitter(.05, factor=2))+
  geom_point(shape=16, size= jitter(4, factor=2), color=&quot;#ffff99&quot;, alpha=jitter(.05, factor=2))+
  geom_point(shape=46, size= 0, color=&quot;#ffff00&quot;)+opt

</pre>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aschinchon.wordpress.com/3253/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/aschinchon.wordpress.com/3253/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-layout="box_count" data-send="true" data-height="62" data-href="https://www.r-bloggers.com/chaotic-galaxies/"></div></div><div><a data-url="https://www.r-bloggers.com/chaotic-galaxies/" data-counturl="https://www.r-bloggers.com/chaotic-galaxies/" data-count="vertical" data-text="Chaotic Galaxies" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://aschinchon.wordpress.com/2016/09/06/chaotic-galaxies/"> Ripples</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/q5Kx1DoatzQ"></a></div>