<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/random-walk-of-pi-another-ggplot2-experiment/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://chichacha.netlify.com/2018/10/14/random-walk-of-pi-another-ggplot2-experiment/"> R on Chi's Impe[r]fect Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>There are so many beautiful “π” arts everywhere, and I wanted to practice ggplot2 by mimicing those arts <a rel="nofollow" href="https://chichacha.netlify.com/2018/10/13/visual-art-with-pi-using-ggplot2-circlize/" target="_blank">further more</a>. Another pi art caught my eye is random walk of pi digits. Here’s one of examples in <a rel="nofollow" href="https://www.wired.com/2012/06/a-random-walk-with-pi/" target="_blank">WIRED magazine</a>.</p>
<p>For random walk to work, I’ve assigned direction to “walk” depending on digits 0-9.</p>
<p><img src="https://i0.wp.com/chichacha.netlify.com/post/2018-10-14-random-walk-of-pi-another-ggplot2-experiment_files/figure-html/unnamed-chunk-1-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>I’ve prepared data frame as below, so I can use ggplot2 to plot</p>
<pre><code>## pi_df contains first 100K digits , each digit is stored in one row.

df_walk &lt;- pi_df %>% 
  mutate(pos = row_number()-1, ## assign position
         dig= as.numeric(dig)) %>%
  select(pos, dig) %>%
  mutate(angle_rad = 2*pi/10*dig,  ## using current digit determine direction to move
         angle_deg = circular::deg(angle_rad), ## I just like to see number in degree...
         move_x = cos(angle_rad), ## how much to move in x direction
         move_y = sin(angle_rad), ## how much to move in y direction
         last_x = replace_na(lag(move_x),0), ## position of last x, set origin as 0
         last_y = replace_na(lag(move_y),0), ## position of last y, set origin as 0
         cumsum_x = cumsum(move_x), ## walkig == adding up all steps in x
         cumsum_y = cumsum(move_y), ## walking == adding up all steps in y
         cumsum_x_lag = cumsum(last_x),
         cumsum_y_lag = cumsum(last_y)) 

df_walk %>% head(n=5) %>% knitr::kable()</code></pre>
<table>
<thead>
<tr>
<th align="right">pos</th>
<th align="right">dig</th>
<th align="right">angle_rad</th>
<th align="right">angle_deg</th>
<th align="right">move_x</th>
<th align="right">move_y</th>
<th align="right">last_x</th>
<th align="right">last_y</th>
<th align="right">cumsum_x</th>
<th align="right">cumsum_y</th>
<th align="right">cumsum_x_lag</th>
<th align="right">cumsum_y_lag</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">0</td>
<td align="right">3</td>
<td align="right">1.8849556</td>
<td align="right">108</td>
<td align="right">-0.309017</td>
<td align="right">0.9510565</td>
<td align="right">0.000000</td>
<td align="right">0.0000000</td>
<td align="right">-0.309017</td>
<td align="right">0.9510565</td>
<td align="right">0.000000</td>
<td align="right">0.0000000</td>
</tr>
<tr>
<td align="right">1</td>
<td align="right">1</td>
<td align="right">0.6283185</td>
<td align="right">36</td>
<td align="right">0.809017</td>
<td align="right">0.5877853</td>
<td align="right">-0.309017</td>
<td align="right">0.9510565</td>
<td align="right">0.500000</td>
<td align="right">1.5388418</td>
<td align="right">-0.309017</td>
<td align="right">0.9510565</td>
</tr>
<tr>
<td align="right">2</td>
<td align="right">4</td>
<td align="right">2.5132741</td>
<td align="right">144</td>
<td align="right">-0.809017</td>
<td align="right">0.5877853</td>
<td align="right">0.809017</td>
<td align="right">0.5877853</td>
<td align="right">-0.309017</td>
<td align="right">2.1266270</td>
<td align="right">0.500000</td>
<td align="right">1.5388418</td>
</tr>
<tr>
<td align="right">3</td>
<td align="right">1</td>
<td align="right">0.6283185</td>
<td align="right">36</td>
<td align="right">0.809017</td>
<td align="right">0.5877853</td>
<td align="right">-0.809017</td>
<td align="right">0.5877853</td>
<td align="right">0.500000</td>
<td align="right">2.7144123</td>
<td align="right">-0.309017</td>
<td align="right">2.1266270</td>
</tr>
<tr>
<td align="right">4</td>
<td align="right">5</td>
<td align="right">3.1415927</td>
<td align="right">180</td>
<td align="right">-1.000000</td>
<td align="right">0.0000000</td>
<td align="right">0.809017</td>
<td align="right">0.5877853</td>
<td align="right">-0.500000</td>
<td align="right">2.7144123</td>
<td align="right">0.500000</td>
<td align="right">2.7144123</td>
</tr>
</tbody>
</table>
<p>Now to visualize the random walk, I’ve used below script to visualize first 1000, first 10000 and first 100000 digits of pi.</p>
<pre><code>n_steps &lt;- 1000
## Random Walk of Pi
df_walk %>% 
  filter(pos &lt; n_steps) %>%
  ggplot(aes(x=cumsum_x, y=cumsum_y, color=pos)) + 
  geom_segment(size=0.5, aes(xend=cumsum_x_lag, yend=cumsum_y_lag)) +
  geom_point(size=0.8) + 
  theme_void(base_family=&quot;Roboto Condensed&quot;) +
  theme(panel.background=element_rect(fill=&quot;#000000&quot;)) +
  scale_color_viridis_c(option=&quot;plasma&quot;, guide=&quot;none&quot;) +
  labs(caption=paste(&quot;First&quot;,n_steps,&quot;digits of Pi&quot;))  +
  geom_hline(yintercept=0, color=&quot;#ffffff30&quot;, linetype=3) +
  geom_vline(xintercept=0, color=&quot;#ffffff30&quot;, linetype=3)</code></pre>
<p><img src="https://i2.wp.com/chichacha.netlify.com/post/2018-10-14-random-walk-of-pi-another-ggplot2-experiment_files/figure-html/ggplot2_fun-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>#ggsave(str_c(&quot;output/random_walk_pi_&quot;,n_steps,&quot;.png&quot;), width=11, height=7)


n_steps &lt;- 10000
## Random Walk of Pi
df_walk %>% 
  filter(pos &lt; n_steps) %>%
  ggplot(aes(x=cumsum_x, y=cumsum_y, color=pos)) + 
  geom_segment(size=0.1, aes(xend=cumsum_x_lag, yend=cumsum_y_lag)) +
  geom_point(size=0.01) + 
  theme_void(base_family=&quot;Roboto Condensed&quot;) +
  theme(panel.background=element_rect(fill=&quot;#000000&quot;)) +
  scale_color_viridis_c(option=&quot;plasma&quot;, guide=&quot;none&quot;) +
  labs(caption=paste(&quot;First&quot;,n_steps,&quot;digits of Pi&quot;))  +
  geom_hline(yintercept=0, color=&quot;#ffffff30&quot;, linetype=3) +
  geom_vline(xintercept=0, color=&quot;#ffffff30&quot;, linetype=3)</code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-10-14-random-walk-of-pi-another-ggplot2-experiment_files/figure-html/ggplot2_fun-2.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>n_steps &lt;- 100000
## Random Walk of Pi
df_walk %>% 
  filter(pos &lt; n_steps) %>%
  ggplot(aes(x=cumsum_x, y=cumsum_y, color=pos)) + 
  geom_segment(size=0.1, aes(xend=cumsum_x_lag, yend=cumsum_y_lag)) +
  geom_point(size=0.005) + 
  theme_void(base_family=&quot;Roboto Condensed&quot;) +
  theme(panel.background=element_rect(fill=&quot;#000000&quot;)) +
  scale_color_viridis_c(option=&quot;plasma&quot;, guide=&quot;none&quot;) +
  labs(caption=paste(&quot;First 100000 digits of Pi&quot;))  +
  geom_hline(yintercept=0, color=&quot;#ffffff30&quot;, linetype=3) +
  geom_vline(xintercept=0, color=&quot;#ffffff30&quot;, linetype=3)</code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-10-14-random-walk-of-pi-another-ggplot2-experiment_files/figure-html/ggplot2_fun-3.png?w=450&ssl=1" data-recalc-dims="1"></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://chichacha.netlify.com/2018/10/14/random-walk-of-pi-another-ggplot2-experiment/"> R on Chi's Impe[r]fect Blog</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/lhCamfdjqsU" height="1"></a></div>