public
Last active

Graphing a subset of isDotR's Twitter ego net.

  • Download Gist
pretty_network.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
doInstall <- TRUE
toInstall <- c("sna", "igraph")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
 
adjacencyList <- read.csv("https://dl.dropbox.com/s/8wabcrtqxysp03u/Twitter_network.R.csv?dl=1")
head(adjacencyList)
 
adjacencyMatrix <- table(adjacencyList)
as.matrix(sort(rowSums(adjacencyMatrix))) # Out-degree
as.matrix(sort(colSums(adjacencyMatrix))) # In-degree
 
# To calculate PageRank, we need an igraph Object
graphObject <- graph.adjacency(adjacencyMatrix)
plot(graphObject) # This plot is not pretty.
 
pageRank <- page.rank(graphObject)$vector
as.matrix(sort(pageRank))
# isDotR has the highest pageRank, because this is basically its ego network
# Note thate pageRank is pretty similar to eigenvector centrality:
plot(pageRank, evcent(graphObject, directed = TRUE)$vector)
 
# Now, to make the prettiest graph we can:
png("Twitter_graph.png", h = 800, w = 800, type = "cairo-png")
par(mai = c(0, 0, 0, 0))
pageRankColor <- hsv(0, 1, (pageRank - min(pageRank)) /
(max(pageRank) - min(pageRank)))
pageRankScaler <- pageRank * 10 + 1/2
prettyPlot <- gplot(dat = adjacencyMatrix,
label = rownames(adjacencyMatrix),
mode = "kamadakawai",
pad = 0,
label.pad = 1,
boxed.labels = TRUE,
label.pos = 1, # Below vertex
label.bg = "#ffffff99",
vertex.sides = 100, # Basically circular
arrowhead.cex = pageRankScaler,
label.cex = pageRankScaler,
vertex.cex = pageRankScaler,
edge.col = "#00000011", # To make translucent bounding box
label.col = pageRankColor,
vertex.col = pageRankColor,
label.border = "#ffffff00", # To hide borders
vertex.border = "#ffffff00") # To hide borders
dev.off()

adjacencyList <- read.csv("https://dl.dropbox.com/s/8wabcrtqxysp03u/Twitter_network.R.csv?dl=1")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : unsupported URL scheme

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.