plotSankey: Sankey Diagram

View source: R/plotSankey.R

plotSankeyR Documentation

Sankey Diagram

Description

plots a sankey diagram

Usage

plotSankey(
  df,
  y,
  vars,
  labels.text,
  label.columns,
  perc,
  col.alluvium,
  col.stratum
)

Arguments

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

Value

returns a ggplot2 object

Examples

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"
)


kelloggs13/nifty documentation built on Nov. 18, 2022, 12:21 a.m.