Heatmaps of the first 100000 digits of pi and e

data viz ggplot2 numbers

Chaos of numbers


Author

Affiliation

Quang Nguyen

 

Published

Aug. 1, 2022

Citation

Nguyen, 2022


I recently saw this Twitter post about a heatmap of the first 100000 digits of π:

Let’s see how closely I can recreate this figure.

First, let’s get data for the first 100000 digits of π, clean it up, and transform it to the right format. I will let the figure size be 400 x 250.

library(tidyverse)
library(rvest)

pi <- read_html("https://www.angio.net/pi/digits/100000.txt") |> 
  html_text() |> 
  str_remove("\\.") 

pi_digit <- tibble(digit = pi) |> 
  mutate(digit = str_split(digit, "")) |> 
  unnest_longer(digit) |> 
  mutate(row_index = rep(1:400, 250),
         col_index = rep(1:250, each = 400))

Now, a few lines of ggplot2 should give the desired visualization.

pi_digit |>
  ggplot(aes(row_index, col_index, fill = factor(digit))) +
  geom_tile() +
  scale_fill_manual(values = rainbow(10)) +
  scale_y_reverse() + 
  guides(fill = guide_legend(nrow = 1, title = NULL)) + 
  coord_equal() +
  theme_void() +
  theme(legend.position = "bottom",
        legend.direction = "horizontal",
        legend.text = element_text(size = 9, margin = margin(l = -17, unit = "pt")))

Breakdown:

Bonus: let’s play the same game and plot the first 100000 digits of e (Euler’s number).

e <- read_html("http://boston.conman.org/2004/11/12/e100k.txt") |> 
  html_text() |> 
  str_remove("^[^\n]*\n") |>  # remove first line 
  str_remove_all("\\s")       # remove all whitespace

# append 2 to the string of first 99999 digits
# since these are digits after the decimal
e_digit <- tibble(digit = str_c(2, str_sub(e, 1, 99999))) |> 
  mutate(digit = str_split(digit, "")) |> 
  unnest_longer(digit) |> 
  mutate(row_index = rep(1:400, each = 250),
         col_index = rep(1:250, 400))

e_digit |>
  ggplot(aes(row_index, col_index, fill = factor(digit))) +
  geom_tile() +
  scale_fill_manual(values = rainbow(10)) +
  scale_y_reverse() + 
  guides(fill = guide_legend(nrow = 1, title = NULL))+ 
  coord_equal() +
  theme_void() +
  theme(legend.position = "bottom",
        legend.direction = "horizontal",
        legend.text = element_text(size = 9, margin = margin(l = -17, unit = "pt")))

Both figures look noisy. There’s no pattern at all for these two famous numbers.

Footnotes

    Citation

    For attribution, please cite this work as

    Nguyen (2022, Aug. 1). The Q: Heatmaps of the first 100000 digits of pi and e. Retrieved from https://qntkhvn.netlify.app/posts/2022-08-01-pi-digits/

    BibTeX citation

    @misc{nguyen2022heatmaps,
      author = {Nguyen, Quang},
      title = {The Q: Heatmaps of the first 100000 digits of pi and e},
      url = {https://qntkhvn.netlify.app/posts/2022-08-01-pi-digits/},
      year = {2022}
    }