Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order y-axis on ggforce::geom_parallel_sets

Tags:

r

ggplot2

ggforce

Using the dev version of the ggforce package, I can create a Sankey diagram as follows (from the documentation)

data <- reshape2::melt(Titanic)
data <- gather_set_data(data, 1:4)

ggplot(data, aes(x, id = id, split = y, value = value)) +
  geom_parallel_sets(aes(fill = Sex), alpha = 0.3, axis.width = 0.1) +
  geom_parallel_sets_axes(axis.width = 0.1) +
  geom_parallel_sets_labels(colour = 'white')

enter image description here

What I'm struggling with, is getting the y-axis variables ordered in any way other than the default, which appears to be reverse alphabetical. For example, changing the plot so Adult appeared near the top of the plot, with Child below.

I've tried re-leveling the factors before applying gather_set_data, as well as re-leveling the y variable after applying gather_set_data, and neither appear to work. I've also tried defining them as characters and sorting in different orders but that also doesn't seem to work.

Any help would be appreciated.

like image 789
Daniel Anderson Avatar asked Dec 11 '17 21:12

Daniel Anderson


2 Answers

Unsure what you would do with ggforce as I don't use this package. I assumed the solution would be to re-level the factors as you mentioned but this doesn't seem to be working for you. However, this does work with ggalluvial. Furthermore, there is an argument reverse that allows you to reverse the order (alphabetical/reverse alphabetical). See below:

Default ordering

library(ggplot2)
library(ggalluvial)

df <- as.data.frame(Titanic)

ggplot(as.data.frame(df),
       aes(weight = Freq,
           axis1 = Survived, axis2 = Sex, axis3 = Class)) +
  geom_alluvium(aes(fill = Class),
                width = 0, knot.pos = 1/4, reverse = FALSE) +
  guides(fill = FALSE) +
  geom_stratum(width = 1/8, reverse = FALSE) +
  geom_text(stat = "stratum", label.strata = TRUE, reverse = FALSE) +
  scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
  ggtitle("Titanic survival by class and sex")

enter image description here

Reverse ordering

ggplot(as.data.frame(df),
       aes(weight = Freq,
           axis1 = Survived, axis2 = Sex, axis3 = Class)) +
  geom_alluvium(aes(fill = Class),
                width = 0, knot.pos = 1/4, reverse = TRUE) +
  guides(fill = FALSE) +
  geom_stratum(width = 1/8, reverse = TRUE) +
  geom_text(stat = "stratum", label.strata = TRUE, reverse = TRUE) +
  scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
  ggtitle("Titanic survival by class and sex")

enter image description here

Re-leveling factor

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

ggplot(as.data.frame(df),
       aes(weight = Freq,
           axis1 = Survived, axis2 = Sex, axis3 = Class)) +
  geom_alluvium(aes(fill = Class),
                width = 0, knot.pos = 1/4, reverse = FALSE) +
  guides(fill = FALSE) +
  geom_stratum(width = 1/8, reverse = FALSE) +
  geom_text(stat = "stratum", label.strata = TRUE, reverse = FALSE) +
  scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
  ggtitle("Titanic survival by class and sex")

enter image description here

like image 88
tyluRp Avatar answered Oct 09 '22 14:10

tyluRp


How about changing y variable into factor as follows?

    titanic <- reshape2::melt(Titanic)
    titanic <- gather_set_data(titanic, 1:4)
    titanic$y <- factor(titanic$y, levels=c("Adult", "Child", "1st", "2nd", "3rd", "Crew", "Male", "Female", "Yes", "No"))
    ggplot(titanic, aes(x, id = id, split = y, value = value)) +
        geom_parallel_sets(aes(fill = Sex), alpha = 0.3, axis.width = 0.1) +
        geom_parallel_sets_axes(axis.width = 0.1) +
        geom_parallel_sets_labels(colour = 'white')
like image 1
Jozef Asai Avatar answered Oct 09 '22 14:10

Jozef Asai