Last active
December 10, 2015 13:56
RSiteCatalyst Sankey Chart - Many-to-Many
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
#Multi-page pathing | |
library("d3Network") | |
library("RSiteCatalyst") | |
#### Authentication | |
SCAuth("name", "secret") | |
#### Get All Possible Paths with ("::anything::", "::anything::") | |
pathpattern <- c("::anything::", "::anything::") | |
next_page <- QueuePathing("zwitchdev", | |
"2014-01-01", | |
"2014-08-31", | |
metric="pageviews", | |
element="page", | |
pathpattern, | |
top = 50000) | |
#Optional step: Cleaning my pagename URLs to remove to domain for clarity | |
next_page$step.1 <- sub("http://randyzwitch.com/","", | |
next_page$step.1, ignore.case = TRUE) | |
next_page$step.2 <- sub("http://randyzwitch.com/","", | |
next_page$step.2, ignore.case = TRUE) | |
#Filter out Entered Site and duplicate rows, >120 for chart legibility | |
links <- subset(next_page, count >= 120 & step.1 != "Entered Site") | |
#Get unique values of page name to create nodes df | |
#Create an index value, starting at 0 | |
nodes <- as.data.frame(unique(c(links$step.1, links$step.2))) | |
names(nodes) <- "name" | |
nodes$nodevalue <- as.numeric(row.names(nodes)) - 1 | |
#Convert string to numeric nodeid | |
links <- merge(links, nodes, by.x="step.1", by.y="name") | |
names(links) <- c("step.1", "step.2", "value", "segment.id", "segment.name", "source") | |
links <- merge(links, nodes, by.x="step.2", by.y="name") | |
names(links) <- c("step.2", "step.1", "value", "segment.id", "segment.name","source", "target") | |
#Create next page Sankey chart | |
d3output = "~/Desktop/sankey_all.html" | |
d3Sankey(Links = links, Nodes = nodes, Source = "source", | |
Target = "target", Value = "value", NodeID = "name", | |
fontsize = 12, nodeWidth = 50, file = d3output, width = 750, height = 700) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment