0

I have this dataset:

type <- c(1, 2, NA, 1, 2, NA)
freq <- c(75, 12, 25, 69, 22, 32)
time <- c("before", "before", "before", "after", "after", "after")

df <- data.frame(type , freq, time )

I need to make a graph like this (with different data obviously):

enter image description here

I tried to follow the guide here: https://cran.r-project.org/web/packages/ggalluvial/vignettes/ggalluvial.html

Using this code:

ggplot(modechoice,
       aes(x = time, stratum = type, alluvium = time,
           y = freq,
           fill = type, label = type)) +
  scale_x_discrete(expand = c(.1, .1)) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 3) +
  theme(legend.position = "none") +
  ggtitle("x")

But I get the error that my data is not recognised as an alluvial. What am I doing wrong?

Victor Nielsen
  • 443
  • 2
  • 14

1 Answers1

0

You should have a fourth variable which is linked with your time variable, that's why I created an extra variable as an example. You can use the following code:

type <- c("1", "2", "NA", "1", "2", "NA")
freq <- c(75, 12, 25, 69, 22, 32)
time1 <- c("before", "before", "before", "after", "after", "after")
time2 <- c("after", "before", "after", "after", "before", "after") 


df <- data.frame(type , freq, time1, time2)

library(tidyverse)
library(ggalluvial)

ggplot(df,
       aes(y = freq, axis1 = time1, axis2 = time2)) +
  geom_alluvium(aes(fill = type), width = 1/12) +
  geom_stratum(width = 1/12, fill = "black", color = "grey") +
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("time1", "time2"), expand = c(.05, .05)) +
  scale_fill_brewer(type = "qual", palette = "Set1") +
  ggtitle("x")

Output:

enter image description here

Edit after Victor Nielson comment:

    type <- c("1", "2", "NA", "1", "2", "NA")
freq <- c(75, 12, 25, 69, 22, 32)
time1 <- c("before", "before", "before", "before", "before", "before")
time2 <- c("after", "after", "after", "after", "after", "after") 


df <- data.frame(type , freq, time1, time2)

library(tidyverse)
library(ggalluvial)

ggplot(df,
       aes(y = freq, axis1 = time1, axis2 = time2)) +
  geom_alluvium(aes(fill = type), width = 1/12) +
  geom_stratum(width = 1/12, fill = "black", color = "grey") +
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("time1", "time2"), expand = c(.05, .05)) +
  scale_fill_brewer(type = "qual", palette = "Set1") +
  ggtitle("x")

Output:

enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • I was thinking that the first (black) column is supposed to be before and the other column is after and that the variables along these columns is the type. – Victor Nielsen Mar 24 '22 at 15:06
  • @VictorNielsen, but then you will get just horizontal paths from left before to right after. – Quinten Mar 24 '22 at 17:28
  • I have two times, before and after. I want to see how the number of type 1 and 2 and NA changes from the before to the after. – Victor Nielsen Mar 25 '22 at 10:16
  • @VictorNielsen, I added some code with two times with before and after. – Quinten Mar 25 '22 at 10:47
  • Not sure what I'm doing wrong. There shouldn't be six, but just three. If you look at the data, there are three entries of freq (one for each type) in the before and three in the after. It looks like you put the same 6 on before and after when there are two sets of 3s. – Victor Nielsen Mar 25 '22 at 15:45