I am adding flow percentages in my ggalluvial plot based on an existing StackOverflow case (how to calculate percentages in geom_flow() based on variable size and not stratum size) which works fine.
-
It seems that the solution does not define rounded percentages, it just puts percentages in the column based on how much there is room for, so in my case I get one decimal whereas the example has no decimals. I would like to have rounded percentages, but I could not figure out how.
-
Also, it would be nice if I could move the percentages slightly on the x-axis so that the ggrepel lines are not on top of the percentages.
dummy dataset
mult_conc2 <- c("mtx", "mtx", "none", "mtx", "multi", "mtx", "none", "multi")
cond_psoriasis <- c(0, 1, 1, 1, 0, 0, 1, 1)
bio_drug_name <- c("rituximab", "anakinra", "rituximab", "baricitinib", "etanercept",
"etanercept", "ustekinumab", "guselkumab")
count <- c(50, 50, 40, 70, 30, 25, 25, 70)
allu5 <- data.frame(mult_conc2, cond_psoriasis, bio_drug_name, count, N)
rm("bio_drug_name", "cond_psoriasis", "count", "mult_conc2")
cols <- c("bio_drug_name", "mult_conc2", "cond_psoriasis")
allu5 <- allu5 %>% mutate_at(cols, factor)
rm(cols)
plot
colorfill <- c("tomato4", "tomato3", "tomato2", "tomato1", "tomato", "turquoise",
"yellow2", "steelblue1", "springgreen", "snow3", "slategray2", "red1", "purple", "plum1",
"olivedrab2")
ggplot(
allu5,
aes(y = count, axis1 = bio_drug_name, axis2 = mult_conc2)
) +
geom_alluvium(aes(fill = bio_drug_name), width = 1 / 12) +
geom_stratum(alpha = 1, width = 1 / 5, fill = "#ffffff", color = "black") +
ggrepel::geom_label_repel(
stat = "stratum", aes(label = after_stat(stratum)), size = 6,
direction = "y",
nudge_x = .25,
label.size = 0
) +
scale_x_discrete(
limits = c("bio drug name", "concomitant"),
expand = c(0.1, 0.1)
) +
scale_fill_manual(values = colorfill) +
guides(fill = "none") +
ggtitle("b-tsDMARD, concommitant treatment and condition") +
theme(legend.position = "none") +
geom_text(stat = "flow", aes(
label = scales::percent(after_stat(count)/after_stat(max(ymax))),
hjust = after_stat(flow) == "to"
))