Created
December 12, 2012 17:39
-
-
Save dsparks/4269893 to your computer and use it in GitHub Desktop.
Graphing a subset of isDotR's Twitter ego net.
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
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() |
ghost
commented
Feb 2, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment