<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/tweetable-mathematical-art-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://fronkonstin.com/2018/09/06/tweetable-mathematical-art-with-r/"> 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%2F2018%2F09%2F06%2Ftweetable-mathematical-art-with-r%2F&via=aschinchon" class="twitter-share-button" target="_blank">Tweet</a></div>
<blockquote>
<p>
Sin ese peso ya no hay gravedad<br>
Sin gravedad ya no hay anzuelo<br>
(Mira cómo vuelo, Miss Caffeina)
</p>
</blockquote>
<p>I love messing around with R to generate mathematical patterns. I always get surprised doing it and gives me lot of satisfaction. I also learn lot of things doing it: not only about R, but also about mathematics. It is one of my favourite hobbies. Some time ago, I published <a rel="nofollow" href="https://fronkonstin.com/2017/12/23/tiny-art-in-less-than-280-characters/" target="_blank">this post</a> showing some drawings, each of them generated with less than 280 characters of code, to be shared on Twitter. This post came to appear in <a rel="nofollow" href="https://news.ycombinator.com/item?id=16422783" target="_blank">Hacker News</a>, which provoked an incredible peak on visits to my blog. Some comments in the Hacker News entry are very interesting.</p>
<p>This Summer I delved into this concept of <em>Tweetable Art</em> publishing several drawings together with the R code to generate them. In this post I will show some.</p>
<p><strong>Vertiginous Spiral</strong></p>
<p>I came up with this image inspired by this nice pattern. It is a turtle graphic inspired pattern but instead of drawing lines I use geom_polygon to colour the resulting image in black and white:</p>
<p><img sizes="(max-width: 604px) 100vw, 604px" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2018/09/vertigo0.png?resize=604%2C604&ssl=1" alt="" width="450" class="wp-image-4651" data-recalc-dims="1"></p>
<p>Code:</p>
<pre>
library(tidyverse)
df &lt;- data.frame(x=0, y=0)
for (i in 2:500){
  df[i,1] &lt;- df[i-1,1]+((0.98)^i)*cos(i)
  df[i,2] &lt;- df[i-1,2]+((0.98)^i)*sin(i)   
}
ggplot(df, aes(x,y)) + 
  geom_polygon()+
  theme_void()
</pre>
<p>Slight modifications of the code can generate appealing patterns like this:</p>
<p><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/vertigo1.png?fit=2400%2C2400&amp;ssl=1" data-orig-size="2400,2400" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/vertigo1.png?fit=300%2C300&amp;ssl=1" data-attachment-id="4652" data-image-meta="{" data-image-description="" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/vertigo1.png?resize=604%2C604&ssl=1" data-permalink="https://fronkonstin.com/2018/09/06/tweetable-mathematical-art-with-r/vertigo1/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/vertigo1.png?fit=604%2C604&amp;ssl=1" alt="" data-image-title="vertigo1" sizes="(max-width: 604px) 100vw, 604px" width="450" data-comments-opened="1" class="wp-image-4652" data-recalc-dims="1"></p>
<p><strong>Marine Creature</strong></p>
<p>A combination of sines and cosines. It reminds me a jellyfish:</p>
<p><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2018/09/vertigo0.png?fit=2400%2C2400&amp;ssl=1" data-orig-size="2400,2400" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2018/09/vertigo0.png?fit=300%2C300&amp;ssl=1" data-attachment-id="4651" data-image-meta="{" data-image-description="" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2018/09/marine.png?resize=604%2C302&ssl=1" data-permalink="https://fronkonstin.com/2018/09/06/tweetable-mathematical-art-with-r/vertigo0/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2018/09/vertigo0.png?fit=604%2C604&amp;ssl=1" alt="" data-image-title="vertigo0" width="450" data-comments-opened="1" class="wp-image-4651" data-recalc-dims="1"></p>
<p>Code:</p>
<pre>
library(tidyverse)
seq(from=-10, to=10, by = 0.05) %>%
  expand.grid(x=., y=.) %>%
  ggplot(aes(x=(x^2+pi*cos(y)^2), y=(y+pi*sin(x)))) +
  geom_point(alpha=.1, shape=20, size=1, color=&quot;black&quot;)+
  theme_void()+coord_fixed()
</pre>
<p><strong>Summoning Cthulhu</strong></p>
<p>The name is inspired in an answer from Mara Averick to <a rel="nofollow" href="https://twitter.com/aschinchon/status/1033793318898028544" target="_blank">this tweet</a>. It is a modification of the marine creature in polar coordinates:</p>
<p><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/cthulthu.png?fit=2700%2C2700&amp;ssl=1" data-orig-size="2700,2700" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/cthulthu.png?fit=300%2C300&amp;ssl=1" data-attachment-id="4648" data-image-meta="{" data-image-description="" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/cthulthu.png?resize=604%2C604&ssl=1" data-permalink="https://fronkonstin.com/2018/09/06/tweetable-mathematical-art-with-r/cthulthu/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/cthulthu.png?fit=604%2C604&amp;ssl=1" alt="" data-image-title="cthulthu" sizes="(max-width: 604px) 100vw, 604px" width="450" data-comments-opened="1" class="wp-image-4648" data-recalc-dims="1"></p>
<p>Code:</p>
<pre>
library(tidyverse)
seq(-3,3,by=.01) %>%
  expand.grid(x=., y=.) %>%
  ggplot(aes(x=(x^3-sin(y^2)), y=(y^3-cos(x^2)))) +
  geom_point(alpha=.1, shape=20, size=0, color=&quot;white&quot;)+
  theme_void()+
  coord_fixed()+
  theme(panel.background = element_rect(fill=&quot;black&quot;))+
  coord_polar()
</pre>
<p><strong>Naive Sunflower</strong></p>
<p>Sunflowers arrange their seeds according a mathematical pattern called phyllotaxis, whic inspires this image. If you want to create your own flowers, you can do <a rel="nofollow" href="https://www.datacamp.com/projects/62?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">this Datacamp’s project</a>. It’s free and will introduce you to the amazing world of <code>ggplot2</code>, my favourite package to create images:</p>
<p><img data-orig-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/sunflower.png?fit=3600%2C3600&amp;ssl=1" data-orig-size="3600,3600" data-medium-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/sunflower.png?fit=300%2C300&amp;ssl=1" data-attachment-id="4650" data-image-meta="{" data-image-description="" src="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/sunflower.png?resize=604%2C604&ssl=1" data-permalink="https://fronkonstin.com/2018/09/06/tweetable-mathematical-art-with-r/sunflower/" data-large-file="https://i2.wp.com/fronkonstin.com/wp-content/uploads/2018/09/sunflower.png?fit=604%2C604&amp;ssl=1" alt="" data-image-title="sunflower" sizes="(max-width: 604px) 100vw, 604px" width="450" data-comments-opened="1" class="wp-image-4650" data-recalc-dims="1"></p>
<p>Code:</p>
<pre>
library(ggplot2)
a=pi*(3-sqrt(5))
n=500
ggplot(data.frame(r=sqrt(1:n),t=(1:n)*a),
       aes(x=r*cos(t),y=r*sin(t)))+
  geom_point(aes(x=0,y=0),
             size=190,
             colour=&quot;violetred&quot;)+
  geom_point(aes(size=(n-r)),
             shape=21,fill=&quot;gold&quot;,
             colour=&quot;gray90&quot;)+
  theme_void()+theme(legend.position=&quot;none&quot;)
</pre>
<p><strong>Silk Knitting</strong></p>
<p>It is inspired by <a rel="nofollow" href="https://fronkonstin.com/2016/03/01/a-silky-drawing-and-a-tiny-experiment/" target="_blank">this other pattern</a>. A lot of <em>almost transparent</em> white points ondulating according to sines and cosines on a dark coloured background:</p>
<p><img data-orig-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2018/09/silky.png?fit=2700%2C2700&amp;ssl=1" data-orig-size="2700,2700" data-medium-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2018/09/silky.png?fit=300%2C300&amp;ssl=1" data-attachment-id="4649" data-image-meta="{" data-image-description="" src="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2018/09/silky.png?resize=604%2C604&ssl=1" data-permalink="https://fronkonstin.com/2018/09/06/tweetable-mathematical-art-with-r/silky-3/" data-large-file="https://i1.wp.com/fronkonstin.com/wp-content/uploads/2018/09/silky.png?fit=604%2C604&amp;ssl=1" alt="" data-image-title="silky" sizes="(max-width: 604px) 100vw, 604px" width="450" data-comments-opened="1" class="wp-image-4649" data-recalc-dims="1"></p>
<p>Code:</p>
<pre>
library(tidyverse)
seq(-10, 10, by = .05) %>%
  expand.grid(x=., y=.) %>%
  ggplot(aes(x=(x+sin(y)), y=(y+cos(x)))) +
  geom_point(alpha=.1, shape=20, size=0, color=&quot;white&quot;)+
  theme_void()+
  coord_fixed()+
  theme(panel.background = element_rect(fill=&quot;violetred4&quot;))
</pre>
<p>Try to modify them and generate your own patterns: it is a very funny way to learn R.</p>
<p><strong>Note:</strong> in order to make them better readable, some of the pieces of code below may have more than 280 characters but removing unnecessary characters (blanks or carriage return) you can reduce them to make them tweetable.</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="https://fronkonstin.com/2018/09/06/tweetable-mathematical-art-with-r/"> 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/aFcw20L0SXQ" height="1"></a></div>