plotSankey | R Documentation |
plots a sankey diagram
plotSankey( df, y, vars, labels.text, label.columns, perc, col.alluvium, col.stratum )
df |
a data.frame containing the number of observations for each flow, cmp. example below |
y |
the number of observations for each flow |
vars |
a string with the variables used as pillars in the chart |
labels.text |
a string with the labels used to name the groups |
label.columns |
a string with a title to describe the pillars |
perc |
todo |
col.alluvium |
todo |
col.stratum |
todo |
returns a ggplot2 object
require(tidyverse) set.seed(2334567) ncust <- 10000 df.cust <- data.frame(customerid = 1:ncust , segment_2000 = sample(LETTERS[1:4], ncust, replace = TRUE, prob = c(0.1, 0.2, 0.5, 0.2)) , segment_2001 = sample(LETTERS[1:4], ncust, replace = TRUE, prob = c(0.3, 0.4, 0.2, 0.1)) ) df.years <- df.cust %>% group_by(segment_2000, segment_2001) %>% summarise(ncust = n()) %>% ungroup() %>% mutate(perccust = ncust / sum(ncust)) df.years %>% plotSankey(y = "ncust" , vars = c("segment_2000", "segment_2001") , labels.text = c("2000", "2001") , label.columns = 'Year' ) df.years %>% plotSankey(y = "perccust" , vars = c("segment_2000", "segment_2001") , labels.text = c("2000", "2001") , perc = TRUE , label.columns = 'Year' ) df.years %>% plotSankey(y = "ncust" , vars = c("segment_2000", "segment_2001") , labels.text = c("2000", "2001") , label.columns = 'Year' , col.alluvium = "blue" , col.stratum = "grey" )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.