Skip to content

Instantly share code, notes, and snippets.

@briatte briatte/00_packages.r
Last active Nov 3, 2018

Embed
What would you like to do?
library(dplyr)
library(rvest)
library(network)
library(sna)
library(ggnetwork)
library(wesanderson)
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 ]
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])
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)
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
geom_edges() +
geom_nodes()
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()
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
You can’t perform that action at this time.