Last active
March 14, 2016 08:24
-
-
Save daroczig/0b68ac0a0bf15b6f7564 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## fetch & parse data from Wikipedia | |
library(XML) | |
wiki <- 'https://en.wikipedia.org/wiki/Chronology_of_computation_of_%CF%80' | |
tables <- readHTMLTable(readLines(wiki), stringsAsFactors = FALSE) | |
## merge data from 4 tables | |
library(data.table) | |
pis <- rbindlist(list( | |
## data data before 1400 from 3rd table extracted by hand | |
fread('Date,Whom,Decimal places(world records in bold) | |
-250,Archimedes, 2 | |
150,Ptolemy,3 | |
263,Liu Hui,4 | |
480,Zu Chongzhi,6', sep = ',', header = TRUE), | |
## no computes | |
tables[[4]][, c(1, 2, 4)], | |
## mainframes | |
tables[[5]][, c(1, 2, 5)], | |
## PCs | |
tables[[6]][, c(1, 2, 5)])) | |
## clean column names | |
setnames(pis, c('date', 'name', 'digits')) | |
## remove rows without valid digits | |
pis[, digits := as.numeric(gsub(',', '', digits))] | |
pis <- pis[!is.na(digits)] | |
## drop non record rows | |
pis <- pis[digits - cummax(digits) == 0] | |
## clean references from names | |
pis[, name := sub('\\[.*\\]', '', name)] | |
## keep only first person's name | |
pis[, name := sub('(.*)( (and|&|,)).*', '\\1', name)] | |
## clean up dates | |
pis[date == 'Late 18th century', date := '1799'] # this is pretty late | |
pis[, date := as.integer(sub('.* ([0-9]*)$', '\\1', date))] | |
## ggplot + repel + emoGG magic | |
library(ggplot2) | |
library(ggrepel); library(grid) | |
library(emoGG) | |
ggplot(pis, aes(date, digits)) + geom_step() + | |
geom_emoji(emoji = '1f370', size = sqrt(pi) / 1e2) + | |
geom_text_repel(aes(label = sprintf('%s (%s)', name, date)), | |
size = pi, max.iter = pi * 1e3) + | |
scale_y_log10(breaks = 10^(0:13)) + | |
scale_x_continuous(breaks = c(-250, 0, 150, 263, 480, 1400, 1600, 1800, 1900, 2000)) + | |
theme_bw() + xlab('') + ylab('Decimal places of π') + | |
ggtitle(expression(atop( | |
'Chronology of computation of π', | |
atop(italic('Based on data fetched from Wikipedia'))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment