Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Last active December 10, 2015 13:56
Show Gist options
  • Save randyzwitch/dbf6b6be8f48ed5ee48d to your computer and use it in GitHub Desktop.
Save randyzwitch/dbf6b6be8f48ed5ee48d to your computer and use it in GitHub Desktop.
RSiteCatalyst Sankey Chart - Many-to-Many
#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