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())