<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/drawing-10-million-points-with-ggplot-clifford-attractors/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://fronkonstin.com/2017/11/07/drawing-10-million-points-with-ggplot-clifford-attractors/"> 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%2F11%2F07%2Fdrawing-10-million-points-with-ggplot-clifford-attractors%2F&via=aschinchon" class="twitter-share-button" target="_blank">Tweet</a></div>
<blockquote>
<p>For me, mathematics cultivates a perpetual state of wonder about the nature of mind, the limits of thoughts, and our place in this vast cosmos (Clifford A. Pickover – The Math Book: From Pythagoras to the 57th Dimension, 250 Milestones in the History of Mathematics)</p>
</blockquote>
<p>I am a big fan of Clifford Pickover and I find inspiration in his books very often. Thanks to him, I discovered <a rel="nofollow" href="https://fronkonstin.com/2014/10/13/beautiful-curves-the-harmonograph/" target="_blank">the harmonograph</a> and the <a rel="nofollow" href="https://fronkonstin.com/2014/12/16/the-awesome-parrondos-paradox/" target="_blank">Parrondo’s paradox</a>, among many other mathematical treasures. Apart of being a great <em>teacher</em>, he also invented a family of strange attractors wearing his name. Clifford attractors are defined by these equations:</p>
<p><img alt="x_{n+1}, =, sin(a, y_{n}), +, c, cos(a, x_{n})  \  y_{n+1}, =, sin(b, x_{n}), +, d, cos(b, y_{n})  \  " title="x_{n+1}, =, sin(a, y_{n}), +, c, cos(a, x_{n})  \  y_{n+1}, =, sin(b, x_{n}), +, d, cos(b, y_{n})  \  " src="https://s0.wp.com/latex.php?latex=x_%7Bn%2B1%7D%5C%2C+%3D%5C%2C+sin%28a%5C%2C+y_%7Bn%7D%29%5C%2C+%2B%5C%2C+c%5C%2C+cos%28a%5C%2C+x_%7Bn%7D%29++%5C%5C++y_%7Bn%2B1%7D%5C%2C+%3D%5C%2C+sin%28b%5C%2C+x_%7Bn%7D%29%5C%2C+%2B%5C%2C+d%5C%2C+cos%28b%5C%2C+y_%7Bn%7D%29++%5C%5C++&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"></p>
<p>There are infinite attractors, since a, b, c and d are parameters. Given four values (one for each parameter) and a starting point <code>(x<sub>0</sub>, y<sub>0</sub>)</code>, the previous equation defines the exact location of the point at step <code>n</code>, which is defined just by its location at <code>n-1</code>; an attractor can be thought as the trajectory described by a particle. This plot shows the evolution of a particle starting at <code>(x<sub>0</sub>, y<sub>0</sub>)=(0, 0)</code> with parameters <code>a=-1.24458046630025</code>, <code>b=-1.25191834103316</code>, <code>c=-1.81590817030519</code> and <code>d=-1.90866735205054</code> along 10 million of steps:</p>
<p><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/10/RomeoAndJuliet.png?fit=480%2C480&amp;ssl=1" data-orig-size="480,480" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/10/RomeoAndJuliet.png?fit=300%2C300&amp;ssl=1" data-attachment-id="4281" data-image-meta="{" data-image-description="" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/11/Image0W.png?resize=480%2C480&ssl=1" data-permalink="https://fronkonstin.com/2017/10/24/a-shiny-app-to-create-sentimental-tweets-based-on-project-gutenberg-books/romeoandjuliet/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/10/RomeoAndJuliet.png?fit=480%2C480&amp;ssl=1" alt="" data-image-title="RomeoAndJuliet" data-comments-opened="1" class="wp-image-4281" data-recalc-dims="1"></p>
<p>Changing parameters is really entertaining. Drawings have a <em>sandy</em> appearance:</p>
<p><img src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/11/Image1B.png?w=604&ssl=1" data-recalc-dims="1"></p>
<p><img src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/11/Image1W.png?w=604&ssl=1" data-recalc-dims="1"></p>
<p><img src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/11/Image2B.png?w=604&ssl=1" data-recalc-dims="1"></p>
<p><img src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/11/Image2W.png?w=604&ssl=1" data-recalc-dims="1"></p>
<p><img src="https://i0.wp.com/fronkonstin.com/wp-content/uploads/2017/11/Image3B.png?w=604&ssl=1" data-recalc-dims="1"></p>
<p><img src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/11/Image3W.png?w=604&ssl=1" data-recalc-dims="1"></p>
<p><img src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/11/Image4B.png?w=604&ssl=1" data-recalc-dims="1"></p>
<p><img src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2017/11/Image4W.png?w=604&ssl=1" data-recalc-dims="1"></p>
<p><img src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2017/11/Image5B.png?w=604&ssl=1" data-recalc-dims="1"></p>
<p>From a technical point of view, the challenge is creating a data frame with all locations, since it must have 10 milion rows and must be <em>populated</em> sequentially. A very fast way to do it is using <code>Rcpp</code> package. To render the plot I use ggplot, which works quite well. Here you have the code to play with Clifford Attractors if you want:</p>
<pre>
library(Rcpp)
library(ggplot2)
library(dplyr)

opt = theme(legend.position  = &quot;none&quot;,
            panel.background = element_rect(fill=&quot;white&quot;),
            axis.ticks       = element_blank(),
            panel.grid       = element_blank(),
            axis.title       = element_blank(),
            axis.text        = element_blank())

cppFunction('DataFrame createTrajectory(int n, double x0, double y0, 
            double a, double b, double c, double d) {
            // create the columns
            NumericVector x(n);
            NumericVector y(n);
            x[0]=x0;
            y[0]=y0;
            for(int i = 1; i &lt; n; ++i) {
            x[i] = sin(a*y[i-1])+c*cos(a*x[i-1]);
            y[i] = sin(b*x[i-1])+d*cos(b*y[i-1]);
            }
            // return a new data frame
            return DataFrame::create(_[&quot;x&quot;]= x, _[&quot;y&quot;]= y);
            }
            ')

a=-1.24458046630025
b=-1.25191834103316 
c=-1.81590817030519 
d=-1.90866735205054

df=createTrajectory(10000000, 0, 0, a, b, c, d)

png(&quot;Clifford.png&quot;, units=&quot;px&quot;, width=1600, height=1600, res=300)
ggplot(df, aes(x, y)) + geom_point(color=&quot;black&quot;, shape=46, alpha=.01) + opt
dev.off()
</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/11/07/drawing-10-million-points-with-ggplot-clifford-attractors/"> 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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/mZoiIAXhdFU" height="1"></a></div>