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):
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?
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:Output:
Edit after Victor Nielson comment:
Output: