Skip to content

Instantly share code, notes, and snippets.

@andrewnguyen42
Created February 24, 2019 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewnguyen42/760e01ad72e7fcb32ed771e0967032ef to your computer and use it in GitHub Desktop.
Save andrewnguyen42/760e01ad72e7fcb32ed771e0967032ef to your computer and use it in GitHub Desktop.
Visualize the first million digits of pi as a phone background
library(dplyr)
library(ggplot2)
library(httr)
library(stringr)
my_pi <- GET(url = 'https://www.angio.net/pi/digits/pi1000000.txt') %>%
content(as="text")
n <- 1000000
str_split(my_pi, "") %>%
.[[1]] %>%
tibble(digit = .) %>%
slice(-2) %>% #remove the annoying decimal
filter(row_number() < n) %>%
mutate(digit = as.numeric(digit)
, n = row_number()
, radians = .5 - ((360/10) * digit)/180
, x = cospi(radians)
, y = sinpi(radians)) %>%
bind_rows(data.frame(digit = NA, n = 0, radians = 0, x = 0, y = 0)
, .) %>%
mutate(x_cum = cumsum(x)
, y_cum = cumsum(y)) %>%
ggplot(aes(x = x_cum, y = y_cum, colour = n)) +
scale_color_gradient(low = 'grey90', high = 'dodgerblue4') +
geom_path() +
theme_void() +
theme(legend.position = "none")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment