如何在geom_alluvium中的axis1和axis2上方添加標題?

相似

當前代碼:
library(ggplot2)
library(ggalluvial)
df = data.frame(
before = factor(c(4,2,3,1,1,1,2,4,2,2,1,4,3), labels = c("a","b","c","d")),
after = factor(c(3,3,2,1,3,4,4,1,1,2,2,4,3), labels = c("a","b","c","d")),
N = c(4,1,1,2,1,2,1,1,1,1,1,1,1)
)
ggplot(df, aes(y = N, axis1 = before, axis2 = after))
geom_alluvium(aes(fill = before))
geom_stratum()
geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum)))
uj5u.com熱心網友回復:
您可以添加一個普通的 old geom_label:
ggplot(df, aes(y = N, axis1 = before, axis2 = after))
geom_alluvium(aes(fill = before))
geom_stratum()
geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum)))
geom_label(inherit.aes = FALSE,
data = data.frame(x = c(1, 2), y = c(19, 19),
label = c('Before', 'After')),
aes(label = label, x = x, y = y))

或者,如果你想讓標簽看起來“高于”情節,你可以這樣做:
ggplot(df, aes(y = N, axis1 = before, axis2 = after))
geom_alluvium(aes(fill = before))
geom_stratum()
geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum)))
annotate('rect', xmin = -Inf, xmax = Inf, ymax = Inf, ymin = 18.5,
fill = 'white')
geom_text(inherit.aes = FALSE,
data = data.frame(x = c(1, 2), y = c(19, 19),
label = c('Before', 'After')),
aes(label = label, x = x, y = y))

Or, if that feels like cheating, you can use annotate:
ggplot(df, aes(y = N, axis1 = before, axis2 = after))
geom_alluvium(aes(fill = before))
geom_stratum()
geom_text(stat = "stratum", mapping = aes(label = after_stat(stratum)))
coord_cartesian(clip = 'off', ylim = c(0, 18))
annotate('text', y = c(20, 20), x = c(1, 2), label = c('Before', 'After'))
theme(plot.margin = margin(30, 10, 10, 10))

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450630.html
