<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/warp-your-data-to-make-it-visually-appealing/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://shape-of-code.coding-guidelines.com/2017/05/04/warp-your-data-to-make-it-visually-appealing/"> The Shape of Code » R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Data plots can sometimes look very dull and need to be jazzed up a bit. Now, nobody’s suggesting that the important statistical properties of the data be changed, but wouldn’t it be useful if the points could be moved around a bit, to create something visually appealing without losing the desired statistical properties?</p>
<p>Readers have to agree that the plot below looks like fun. Don’t you wish your data could be made to look like this?</p>
<p><img alt="Rabbit image" src="https://i2.wp.com/www.coding-guidelines.com/images/rabbit.png?w=456" data-recalc-dims="1"></p>
<p>Well, now you can (<a target="_blank" rel="nofollow" href="http://www.coding-guidelines.com/code-data/warp-pts.tgz">code here</a>, inspired by <a target="_blank" rel="nofollow" href="https://www.autodeskresearch.com/publications/samestats">Matejka and Fitzmaurice</a> who have not released their code yet). It is also possible to <a target="_blank" rel="nofollow" href="http://shape-of-code.coding-guidelines.com/2016/12/16/automatically-generated-join-the-dots-images/">thin-out the points, while maintaining the visual form</a> of the original image.</p>
<p>The idea is to perturb the <code>x</code>/<code>y</code> position of very point by a small amount, such that the desired statistical properties are maintained to some level of accuracy:</p>
<div>
<table>
<tr>
<td>
<pre>check_prop=function(new_pts, is_x)
{
if (is_x)
   return(abs(myx_mean-stat_cond(new_pts)) &lt; 0.01)
else
   return(abs(myy_mean-stat_cond(new_pts)) &lt; 0.01)
}
 
 
mv_pts=function(pts)
{
repeat
   {
   new_x=pts$x+runif(num_pts, -0.01, 0.01)
   if (check_prop(new_x, TRUE))
      break()
   }
 
repeat
   {
   new_y=pts$y+runif(num_pts, -0.01, 0.01)
   if (check_prop(new_y, FALSE))
      break()
   }
 
return(data.frame(x=new_x, y=new_y))
}</pre>
</td>
</tr>
</table>
</div>
<p>The distance between the perturbed points and the positions of the target points then needs to be calculated. For each perturbed point its nearest neighbor in the target needs to be found and the distance calculated. This can be done in <img title="n log n" alt="n log n" src="https://i1.wp.com/shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_994_acf9dd6ba8102b8503f545759833c0a5.png?w=456" data-recalc-dims="1"> using <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/K-d_tree">kd-trees</a> and of course there is an R package, <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/RANN/">RANN</a>, do to this (implemented in the <code>nn2</code> function). The following code tries to minimize the sum of the distances, another approach is to minimize the mean distance:</p>
<div>
<table>
<tr>
<td>
<pre>mv_closer=function(pts)
{
repeat
   {
   new_pts=mv_pts(pts)
   new_dist=nn2(rabbit, new_pts, k=1)
   if (sum(new_dist$nn.dists) &lt; cur_dist)
      {
      cur_dist &lt;- sum(new_dist$nn.dists)
      return(new_pts)
      }
   }
 
}</pre>
</td>
</tr>
</table>
</div>
<p>Now it’s just a matter of iterating lots of times, existing if the distance falls below some limit:</p>
<div>
<table>
<tr>
<td>
<pre>iter_closer=function(tgt_pts, src_pts)
{
cur_dist &lt;- sum(nn2(tgt_pts, src_pts, k=1)$nn.dists)
cur_pts=src_pts
for (i in 1:5000)
   {
   new_pts=mv_closer(cur_pts)
   cur_pts=new_pts
   if (cur_dist &lt; 13)
      return(cur_pts)
   }
return(cur_pts)
}</pre>
</td>
</tr>
</table>
</div>
<p>This code handles a single statistical property. Matejka and Fitzmaurice spent more than an hour on their implementation, handle multiple properties and use <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Simulated_annealing">simulated annealing</a> to prevent being trapped in local minima.</p>
<p>An example, with original points in yellow:</p>
<p><img alt="Warp towards rabbit image" src="https://i2.wp.com/www.coding-guidelines.com/images/warp-rabbit.png?w=456" data-recalc-dims="1"></p>
<p>Enjoy.</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://shape-of-code.coding-guidelines.com/2017/05/04/warp-your-data-to-make-it-visually-appealing/"> The Shape of Code » R</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/kYAv80kegHo"></a></div>