不确定你会做什么,ggforce
因为我不使用这个包。我认为解决方案是重新调整您提到的因素,但这似乎对您不起作用。但是,这确实适用于ggalluvial
. 此外,还有一个参数reverse
允许您颠倒顺序(字母顺序/逆字母顺序)。见下文:
默认排序
library(ggplot2)
library(ggalluvial)
df <- as.data.frame(Titanic)
ggplot(as.data.frame(df),
aes(weight = Freq,
axis1 = Survived, axis2 = Sex, axis3 = Class)) +
geom_alluvium(aes(fill = Class),
width = 0, knot.pos = 1/4, reverse = FALSE) +
guides(fill = FALSE) +
geom_stratum(width = 1/8, reverse = FALSE) +
geom_text(stat = "stratum", label.strata = TRUE, reverse = FALSE) +
scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
ggtitle("Titanic survival by class and sex")
逆序
ggplot(as.data.frame(df),
aes(weight = Freq,
axis1 = Survived, axis2 = Sex, axis3 = Class)) +
geom_alluvium(aes(fill = Class),
width = 0, knot.pos = 1/4, reverse = TRUE) +
guides(fill = FALSE) +
geom_stratum(width = 1/8, reverse = TRUE) +
geom_text(stat = "stratum", label.strata = TRUE, reverse = TRUE) +
scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
ggtitle("Titanic survival by class and sex")
再调平系数
df$Class <- factor(df$Class, levels = c("3rd", "1st", "Crew", "2nd"))
ggplot(as.data.frame(df),
aes(weight = Freq,
axis1 = Survived, axis2 = Sex, axis3 = Class)) +
geom_alluvium(aes(fill = Class),
width = 0, knot.pos = 1/4, reverse = FALSE) +
guides(fill = FALSE) +
geom_stratum(width = 1/8, reverse = FALSE) +
geom_text(stat = "stratum", label.strata = TRUE, reverse = FALSE) +
scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
ggtitle("Titanic survival by class and sex")