Skip to content

Instantly share code, notes, and snippets.

@dashalom
Last active Aug 29, 2015
Embed
What would you like to do?
Closest amphitheaters and cities to Rome
#load libraries
library(curl)
library(igraph)
library(rgeos)
library(rgdal)
library(maptools)
library(sp)
library(ggplot2)
library(leaflet)
#load data sets of nodes and edges
orbis_edges <-read.csv(curl("https://stacks.stanford.edu/file/druid:mn425tz9757/orbis_edges_0514.csv"),head=TRUE)
orbis_nodes<-read.csv(curl("https://stacks.stanford.edu/file/druid:mn425tz9757/orbis_nodes_0514.csv"),head=TRUE)
#subset two columns from orbis_nodes.
temp.df <- orbis_nodes[,c("id","label")]
#create the network
o.nw <- graph.data.frame(orbis_edges, vertices=temp.df, directed=TRUE)
#finding the shortest paths referring to Roma, by expenses, and assigning to a new object
shortest.paths(o.nw,V(o.nw)[V(o.nw)$label == "Roma"], weights = E(o.nw)$expense) -> o.nw.torome
o.nw <- set.vertex.attribute(o.nw, "torome", index = V(o.nw), value = o.nw.torome)
# divide into equal fiftieths (or choose another number)
# first print it
quantile(V(o.nw)$torome, probs = 1:50/50)
# now keep it for future use
quantile(V(o.nw)$torome, probs = 1:50/50) -> breaks
# subgraph by various values of torome
tmp.nw <- subgraph(o.nw, v = V(o.nw)[torome <= breaks[1]])
"
plot(tmp.nw,
edge.arrow.size = 0,
vertex.label.cex = .6
)
"
tmp.nw <- subgraph(o.nw, v = V(o.nw)[torome <= breaks[2]])
"
plot(tmp.nw,
edge.arrow.size = 0,
vertex.label.cex = .6
)
"
tmp.nw <- subgraph(o.nw, v = V(o.nw)[torome <= breaks[3]])
"
plot(tmp.nw,
edge.arrow.size = 0,
vertex.label.cex = .6,
vertex.label.color = 'red'
)
"
tmp.nw <- subgraph(o.nw, v = V(o.nw)[torome <= breaks[5]])
"
plot(tmp.nw,
edge.arrow.size = 0,
vertex.label.cex = .6,
vertex.size = 2
)
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment