2

I am trying to make an alluvial chart using ggalluvial on R. I was wondering if it is possible to have different colours for each segment (or between axis).

In the example below, I have A on axis one going to B and C on axis 2. I would like two colours for that. Then from axis 2 to 3, I have B going to B (so the same colour as before), then C going to D, E and F (so three different colours).

What I am getting so far is the Figure below, with all colours at the end (K) going to the begging (A). Is that possible, or geom_sankey would be a more appropriate option?

library(ggalluvial)
library(ggplot2)
library(dplyr)
library(ggeasy)

test <- data.frame(
  one = c("A", "A", "A", "A", "A", "A"),
  two = c("B", "C", "C", "C", "C", "C"),
  three = c("B", "D", "E", "F", "F", "F"),
  four = c("B", "D", "E", "F", "F", "G"),
  five = c("B", "D", "E", "I", "J", "G"),
  six = c("B", "D", "E", "I", "K", "K"),
  y = c(102, 89, 48, 75, 826, 7394)
)

test$two <- factor(test$two, levels = c("B", "C"))

test$three <- factor(test$three, levels = c("B", "D", "E", "F"))

test$four <- factor(test$four, levels = c("B", "D", "E", "F", "G"))

test$five <- factor(test$five, levels = c("B", "D", "E", "I", "J", "G"))

test$six <- factor(test$six, levels = c("B", "D", "E", "I", "K"))

g <- ggplot(data = test, aes(
  axis1 = one,
  axis2 = two,
  axis3 = three,
  axis4 = four,
  axis5 = five,
  axis6 = six,
  y = y
)) +
  geom_alluvium(aes(fill = six), curve_type = "cubic",width = 1 / 4, aes.bind = TRUE) +
  geom_stratum(width = 1 / 4, color = "black", alpha = 0.25) +
  geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
  theme_classic() +
  theme(text = element_text(family = "serif", size = 30, color = "black")) +
  theme(axis.text.x = element_text(size = 30, color = "black")) +
  theme(axis.ticks.x = element_blank()) +
  theme(axis.line.x = element_blank()) +
  theme(legend.position = "none") +
  easy_remove_y_axis()+
  easy_remove_x_axis()

g

enter image description here

0 Answers0