Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created September 24, 2025 08:28
Show Gist options
  • Select an option

  • Save abikoushi/81432efc8519740e848ba605370e83cb to your computer and use it in GitHub Desktop.

Select an option

Save abikoushi/81432efc8519740e848ba605370e83cb to your computer and use it in GitHub Desktop.
tried using ggalluvial
#install.packages("ggalluvial")
library("ggalluvial")
library("dplyr")
V_row=matrix(rexp(100*10),100,10)
V_col=matrix(rexp(100*10),100,10)
W_row = matrix(rexp(10*5),10,5)
cl_row = apply(V_row%*%W_row,1,which.max)
W_col = matrix(rexp(10*7),10,7)
cl_col = apply(V_col%*%W_col,1,which.max)
latent_row = apply(V_row,1,which.max)
latent_col = apply(V_col,1,which.max)
count_row = data.frame(cl_row, latent_row) %>%
group_by_all() %>%
tally() %>%
ungroup()
count_col = data.frame(latent_col, cl_col) %>%
group_by_all() %>%
tally() %>%
ungroup()
jointdf = full_join(count_row , count_col,
by=c("latent_row"="latent_col"),
relationship = "many-to-many") %>%
rename(latent = latent_row)
jointdf = mutate(jointdf, p_row = n.x/sum(n.x), p_col = n.y/sum(n.y)) %>%
mutate(p=p_row*p_col) %>%
mutate(p=p/sum(p))
ggplot(data = jointdf,
aes(axis1 = paste("r", cl_row, sep = "-"),
axis2 = paste("l", latent, sep="-"),
axis3 = paste("c", cl_col, sep="-"), y = p)) +
scale_x_discrete(limits = c("cl_row", "latent", "cl_col"),
expand = c(.2, .05)) +
geom_alluvium(aes(fill=paste("l", latent, sep="-")), show.legend = FALSE)+
geom_stratum() +
geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
labs(y="relative frequnency")+theme_minimal(18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment