Search code examples
rggplot2plotcluster-analysisggalluvial

Data is not in a recognized alluvial form


I am trying to do an alluvial plot which compares ordination methods after clustering. The idea is to have 3 variables on the x axis, corresponding to PCA, PCoA and PLN Model.

I would like to "cut" each one of these into 4 parts (4 clusters given by my hierarchical clustering).

Here is my dataset :

data_alluvial <- read.table(text = "
Var1 Freq method
1     1    4    ACP
2     2    9    ACP
3     3    8    ACP
4     4    4    ACP
5     1    6 PLNPCA
6     2    7 PLNPCA
7     3    5 PLNPCA
8     4    7 PLNPCA
9     1    8   PCoA
10    2    4   PCoA
11    3    9   PCoA
12    4    4   PCoA", header = TRUE)

and here is what I tried :

ggplot(data = data_alluvial, aes(x = method, stratum = Var1, alluvium = Freq, label = Var1)) + geom_alluvium(aes(fill = Var1))

I get the message :

Data is not in a recognized alluvial form (see help('alluvial-data') for details).

I don't really get what is wrong with my dataset...can someone help me ?


Solution

  • Try this:

    #convert to factor
    data_alluvial$Var1 <- as.factor(data_alluvial$Var1)
    
    ggplot(data = data_alluvial,
           aes(x = method, y = Freq, alluvium = Var1, 
               fill = Var1, stratum = Var1)) +
      geom_alluvium() +
      geom_stratum()
    

    enter image description here