Created
September 8, 2014 00:28
Force-directed network with RSiteCatalyst
This file contains 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
#### Force directed network | |
#Limit to more than 5 occurence like in simple network | |
fd_graph_links <- subset(graph_links, count > 5) | |
#Get unique values of page name to create nodes df | |
#Create an index value, starting at 0 | |
fd_nodes <- as.data.frame(unique(c(fd_graph_links$step.1, fd_graph_links$step.2))) | |
names(fd_nodes) <- "name" | |
fd_nodes$nodevalue <- as.numeric(row.names(fd_nodes)) - 1 | |
#Create groupings for node colors | |
#This is user-specific in terms of how to create these groupings | |
#Due to few number of pages/topics, I am manually coding this | |
grouping <- function(string){ | |
if(grepl("(hadoop|hive|pig)",string, perl=TRUE)){ | |
return(1) | |
}else if(grepl("(julia|uaparser-jl)",string, , perl=TRUE)){ | |
return(2) | |
}else if(grepl(("[r]?sitecatalyst|adobe-analytics|omniture"),string, perl=TRUE)){ | |
return(3) | |
}else if(grepl("(wordpress|twenty-eleven|scrappy)",string, perl=TRUE)){ | |
return(4) | |
}else if(grepl("data-science|ec2",string, perl=TRUE)){ | |
return(5) | |
}else if(grepl("python",string, perl=TRUE)){ | |
return(6) | |
}else if(grepl("(digital-analytics|google-analytics|web-analyst)",string, perl=TRUE)){ | |
return(8) | |
}else if(grepl("(macbook|iphone)",string, perl=TRUE)){ | |
return(9) | |
}else if(grepl("(randyzwitch|about|page)",string, perl=TRUE)){ | |
return(10) | |
}else if(grepl("(rstudio|rcmdr|r-language|jsonlite|r-language-oddities|tag/r|automated-re-install-of-packages-for-r-3-0|learning-r-sas|creating-dummy-variables-data-frame-r)",string, perl=TRUE)){ | |
return(7) | |
}else{ | |
return(11) | |
} | |
} | |
#Create group column | |
fd_nodes$group <- sapply(fd_nodes$name, grouping) | |
#Append numeric nodeid to pagename | |
fd_graph_links <- merge(fd_graph_links, fd_nodes[,1:2], by.x="step.1", by.y="name") | |
names(fd_graph_links) <- c("step.1", "step.2", "value", "source") | |
fd_graph_links <- merge(fd_graph_links, fd_nodes[,1:2], by.x="step.2", by.y="name") | |
names(fd_graph_links) <- c("step.1", "step.2", "value", "source", "target") | |
d3output = "C:/Users/rzwitc200/Desktop/fd_graph.html" | |
# Create force-directed graph | |
d3ForceNetwork(Links = fd_graph_links, Nodes = fd_nodes, Source = "source", | |
Target = "target", NodeID = "name", | |
Group = "group", opacity = 0.8, Value = "value", | |
file = d3output, | |
charge = -90, | |
fontsize=12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment