我正在尝试使用冲积图(桑基图)来显示两个时间段内不同类别的变化。当所有因子水平都在两个时间段(前后)中表示时,我能够创建一个对我有意义的图,但是根据我的数据,在更改因子的顺序后,该图看起来很奇怪。我还想为两个时间段的类别显示相同的填充颜色,但只能更改第一个时间段(前)。当我绘制绘图时,我注意到我指定的颜色不是我想要的每个因子水平的颜色,尽管框/层的顺序是正确的。
当类别在两个时间段内没有完全代表时,关于如何改进情节以及如何克服从两组中排序因子水平的问题的任何帮助或建议都会非常有帮助。
这是代码:
db <- read.table(text = "pre post freq
NE NE 0
NE DD 2
NE LC 5
NE NT 2
NE VU 3
NE EN 5
NE CR 1
DD NE 0
DD DD 3
DD LC 37
DD NT 10
DD VU 14
DD EN 3
DD CR 3
LC NE 0
LC DD 0
LC LC 18
LC NT 2
LC VU 1
LC EN 2
LC CR 0
NT NE 0
NT DD 1
NT LC 3
NT NT 8
NT VU 13
NT EN 5
NT CR 1
VU NE 0
VU DD 0
VU LC 1
VU NT 0
VU VU 7
VU EN 8
VU CR 3
EN NE 0
EN DD 0
EN LC 0
EN NT 0
EN VU 0
EN EN 0
EN CR 2
CR NE 0
CR DD 0
CR LC 1
CR NT 0
CR VU 0
CR EN 0
CR CR 2
", header=T)
head(db)
# Order factor levels
levels(db$pre) <- c("NE", "DD", "LC", "NT", "VU", "EN", "CR")
levels(db$post) <- c("NE", "DD", "LC", "NT", "VU", "EN", "CR")
# Set colors for the plot
colors.p <- c("#282828", "#7C7C7C", "#20AB5F", "#3EFF00",
"#FBFF00", "#FFBD00", "#FF0C00")
# Plot
p <- ggplot(db,
aes(y = freq, axis1 = pre,
axis2 = post)) +
geom_alluvium(aes(fill = pre), show.legend = FALSE) +
geom_stratum(aes(fill = pre), color = "black", alpha = 0.5) +
geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
scale_x_discrete(limits = c("previous", "current"),
expand = c(0.3, 0.01)) +
scale_fill_manual(values = colors.p) +
theme_void() +
theme(
panel.background = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(size = 15, face = "bold"),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.position = "none"
)
p