Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.