Reputation: 107
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.
Data:
allu5 = data.frame(mult_conc2 = as.factor(c("mtx", "mtx", "none", "mtx", "multi", "mtx", "none", "multi")),
cond_psoriasis = as.factor(c(0, 1, 1, 1, 0, 0, 1, 1)),
bio_drug_name = as.factor(c("rituximab", "anakinra", "rituximab", "baricitinib",
"etanercept", "etanercept", "ustekinumab", "guselkumab")),
count = c(50, 50, 40, 70, 30, 25, 25, 70))
colorfill = c("tomato4", "tomato3", "tomato2", "tomato1", "tomato", "turquoise",
"yellow2", "steelblue1", "springgreen", "snow3", "slategray2", "red1", "purple", "plum1",
"olivedrab2")
Attempt:
library(ggplot2)
library(ggalluvial)
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"))
Upvotes: 0
Views: 74
Reputation: 125418
You can set your desired accuracy using the accuracy=
parameter of scales::percent
. And to fix the issue with alignment set hjust=1
to align all labels on the right (In you code the alignment was set conditionally, i.e. the labels on the left were aligned to the left).
library(ggalluvial)
#> Loading required package: ggplot2
library(ggplot2)
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)),
accuracy = 1
),
hjust = 1
))
Upvotes: 2