Last active
November 3, 2018 02:30
-
-
Save briatte/e4f7934e0ac598ad7fd6 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
library(dplyr) | |
library(rvest) | |
library(network) | |
library(sna) | |
library(ggnetwork) | |
library(wesanderson) |
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
z = "icelandic-legal-code.zip" | |
u = "http://www.althingi.is/lagasafn/zip/nuna/allt.zip" | |
if (!file.exists(z)) | |
download.file(u, z, mode = "wb") | |
f = unzip(z, list = TRUE) | |
f = f[ grepl("\\d{4}(.*)html$", f$Name), 1 ] |
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
d = data_frame() | |
e = data_frame() | |
for (i in rev(f)) { | |
h = read_html(unz(z, i)) | |
d = rbind(d, data_frame(title = html_node(h, "h2") %>% html_text, | |
date = html_node(h, xpath = "//p[2]") %>% html_text)) | |
j = html_nodes(h, xpath = "//a") %>% html_attr("href") | |
j = j[ grepl("^\\d{4}(.*)html(#\\w+)?$", j) ] | |
if (length(j)) | |
e = rbind(e, data_frame(i, j)) | |
} | |
e = filter(e, i != j) | |
e$i = gsub("(.*)\\.html(.*)?", "\\1", e$i) | |
e$j = gsub("(.*)\\.html(.*)?", "\\1", e$j) | |
e = apply(e, 1, function(x) paste0(sort(x), collapse = "_")) | |
e = as.data.frame(table(e)) | |
e = data_frame(i = gsub("(.*)_(.*)", "\\1", e[, 1]), | |
j = gsub("(.*)_(.*)", "\\2", e[, 1]), | |
w = e[, 2]) |
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
n = network(e[, 1:2 ], directed = TRUE) # directed graph | |
set.edge.attribute(n, "crossrefs", e$w) # number of cross-references | |
n %v% "year" = substr(network.vertex.names(n), 1, 4) # year of legislation | |
n %v% "degree" = degree(n) # Freeman degree | |
# equal-n time periods | |
t = cut_number(as.integer(n %v% "year"), 4, dig.lab = 4, right = FALSE) | |
n %v% "period" = as.character(t) |
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
ggplot(n, aes(x, y, xend = xend, yend = yend)) + | |
geom_edges() + | |
geom_nodes() |
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
ggplot(fortify(n, arrow.gap = 0.01), aes(x, y, xend = xend, yend = yend)) + | |
geom_edges(arrow = arrow(length = unit(0.3, "lines")), alpha = 0.5) + | |
geom_nodes(aes(size = degree), shape = 21, fill = "gold", color = "tomato") + | |
theme_blank() |
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
ggplot(fortify(n, arrow.gap = 0), aes(x, y, xend = xend, yend = yend)) + | |
geom_edges(color = "white", alpha = 0.5) + | |
geom_nodes(aes(color = period, size = log10(degree))) + | |
guides(alpha = FALSE, size = FALSE) + | |
scale_color_manual(values = wes_palette("Zissou")[ c(1, 3, 4, 5)]) + | |
theme_blank(14) + | |
theme(rect = element_rect(fill = "grey10"), | |
panel.background = element_rect(fill = "grey10"), | |
text = element_text(color = "white"), | |
legend.position = "bottom") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment