0

I am trying to create an alluvial plot using ggplot and the ggalluvial. I am less interested in the frequency change per position, but instead, I'm more interested in the percent change per position. Is there a way to change my plot from frequency on the y-axis to percent? Appreciate the help.

I created a simulated dataset. Note that I randomly delete 10% of the simulated dataset so that each class frequency changes per position.

id <-  rep(1:15000, each = 7)
position <-  rep(1:7, 15000)
data <- data.frame(id,position)
data$class <- sample(1:4, nrow(data), replace = TRUE)

#randomly eliminating rows so that the number of observations per class is not constant data$random_number <- runif(nrow(data), 0, 1) data <- data[data$random_number >= 0.1, ]

ggplot(data, aes(x = as.factor(position), stratum = as.factor(class), alluvium = as.factor(id), fill = as.factor(class), label = as.factor(class))) + scale_fill_brewer(type = "qual", palette = "Set2") + scale_x_discrete(expand = c(.1, .1)) + geom_flow() + geom_stratum(alpha = .5) + geom_text(stat = "stratum", size = 3) + theme_bw() + theme(legend.position = "none", axis.title=element_blank(),axis.text.y=element_blank())

enter image description here

  • This might get a better reception at StackOverflow or Data Science/Cross Validated (can never keep those two straight). – cmw Sep 19 '23 at 00:21

0 Answers0