'How can I reorder every axis in ggalluvial?

I am trying to clean up the alluvian plot using ggplot. As you can see the graph I plot, all of the columns are in ascending order. I have tried to drag each cell by mouse, but it doesn't work. How can I customize order in every axis? The order of the left column is in ascending order

If this is not possible in ggalluvial but possible in other packages, I'd like to know as well.

Thanks in advance.

My code to plot the graph :


exceldata <- read.xlsx("C:/Users/MENGL/Downloads/kuan1.xlsx",sheetName ="Sheet1" )     
dfdata = data.frame(exceldata)
dfdata

ggplot(data = dfdata,
       aes(
           axis1 = Tool.name,   
           axis2 = Core.network.model,
           axis3 = Evaluation.strategy,  
           axis4 = Published.year,
           y = freq
           )) +
  geom_alluvium(aes(fill = PTM.type)) +
  geom_stratum( width = 0.5) +
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum)),
            size = 4,
            min.y = 1) +
  theme_void()

I have also attached a link towards the sample data.

https://docs.google.com/spreadsheets/d/1fBr6BJuWatcznKB58RM4Okf6tdBto0r9/edit?usp=sharing&ouid=102065784261310303747&rtpof=true&sd=true



Solution 1:[1]

OP, you reorder axes the same way in ggalluvial that you would any other discrete axis or legend ordering in ggplot2: setting the levels of the factor.

Here's an example below modified from an example in the package:

library(ggplot2)
library(ggalluvial)

df <- as.data.frame(Titanic)

ggplot(df, aes(y = Freq, axis1 = Survived, axis2 = Sex, axis3 = Class)) +
  geom_alluvium(
    aes(fill = Class), show.legend=FALSE,
    width = 0, knot.pos = 0, reverse = FALSE) +
  geom_stratum(width = 1/8, reverse = FALSE) +
  geom_text(
    stat = "stratum", aes(label = after_stat(stratum)),
    reverse = FALSE) +
  scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class"))

enter image description here

You can refactor any of the columns and use levels= to specify the ordering:

df <- as.data.frame(Titanic)
df$Class <- factor(df$Class, levels=c("2nd", "Crew", "1st", "3rd"))

ggplot(df, aes(y = Freq, axis1 = Survived, axis2 = Sex, axis3 = Class)) +
  geom_alluvium(
    aes(fill = Class), show.legend=FALSE,
    width = 0, knot.pos = 0, reverse = FALSE) +
  geom_stratum(width = 1/8, reverse = FALSE) +
  geom_text(
    stat = "stratum", aes(label = after_stat(stratum)),
    reverse = FALSE) +
  scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class"))

enter image description here

Addendum (OP request)

OP requested some follow up questions that are pretty straightforward:

  1. How do you change the label of the stratum (ex. "Survived" "Sex" and "Class" in the example)?
  2. How do you specify a set of colors for the alluvials?

The strata are mapped on a continuous x axis in the final plot. Consequently, you approach renaming the strata the same way you would approach relabeling tick marks on the x axis via scale_x_continuous() and specifying breaks= and labels= arguments.

For color, you can specify colors using scale_fill_manual() and supplying the values= argument. For manual specification, it's important that the length of the vector sent to values= matches the number of different things mapped to the fill aesthetic. If specifying hex color codes, you need to specify in the format "#XXXXXX" as a character, starting with a pound sign "#".

ggplot(df, aes(y = Freq, axis1 = Survived, axis2 = Sex, axis3 = Class)) +
  geom_alluvium(
    aes(fill = Class), show.legend=FALSE,
    width = 0, knot.pos = 0, reverse = FALSE) +
  geom_stratum(width = 1/8, reverse = FALSE) +
  geom_text(
    stat = "stratum", aes(label = after_stat(stratum)),
    reverse = FALSE) +
  scale_x_continuous(breaks = 1:3, labels = c("Example Label", "Sex", "Class")) +
  scale_fill_manual(values = c("#2E9599","#F7DC68","#F46C3F", "#A7226F"))

enter image description here

Note: if you want to specifically assign a particular color to a particular alluvial, you can use a named vector. So, something like the following applies colors specifically:

scale_fill_manual(
  values = c(
    "1st" = "red",
    "2nd" = "blue",
    "3rd" = "yellow",
    "Crew" = "green"))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1