Search code examples
rggplot2ggalluvial

Multistep alluvial diagram in R


I have a data frame in R with 6 categories Pearson1, Spearman1, Kendall1, Pearson2, Spearman2, and Kendall2 and I have 6 variables X1, X2, X3, X4, X5 and X6. In each category I have the ranking of the variables from highest to lowest, for example X1 appear as the least significant in all categories (6 placement). enter image description here

df <- data.frame(Variable = c("X1", "X2", "X3", "X4", "X5", "X6"),
                 Pearson1 = c(6, 3, 2, 5, 4, 1),
                 Spearman1 = c(6, 5, 1, 2, 3, 4),
                 Kendall1 = c(6, 5, 1, 2, 3, 4),
                 Pearson2 = c(6, 5, 1, 2, 3, 4),
                 Spearman2 = c(6, 5, 1, 2, 4, 3),
                 Kendall2 = c(6, 5, 1, 2, 3, 4))

I want to create an alluvial diagram with the variables that goes from one step to the other. I want in the first column (step) to have the variables and then seeing the ranking it the 6 steps. My final result looks like this but only black and white with different textures for each variable if thats possible. enter image description here

I have tried the following but it's not working

df_long <- reshape2::melt(df, id.vars = "Variable")
    alluvial(df_long, col = "Variable", freq = "value",
             group = "Variable", border = "white",
             hide = c("Variable"))

Solution

  • Using the first example from the documentation as a code template, and adding a "freq" column to the sample df, makes this chart. No reshaping required.

    df <- data.frame(Variable = c("X1", "X2", "X3", "X4", "X5", "X6"),
                     Pearson1 = c(6, 3, 2, 5, 4, 1),
                     Spearman1 = c(6, 5, 1, 2, 3, 4),
                     Kendall1 = c(6, 5, 1, 2, 3, 4),
                     Pearson2 = c(6, 5, 1, 2, 3, 4),
                     Spearman2 = c(6, 5, 1, 2, 4, 3),
                     Kendall2 = c(6, 5, 1, 2, 3, 4))
    df$freq<-1
    alluvial(df[1:7], freq=df$freq, cex = 0.7)
    

    a beautiful alluvial chart


    Reverse vertical order of furst column:

    alluvial(df[1:7], freq=df$freq,
             cex = 0.7, 
             ordering = list(
               order(df$Variable, decreasing=TRUE),
               NULL,
               NULL,
               NULL,
               NULL,
               NULL,
               NULL
             )
    )