我正在使用ggalluvial生成一个图形,并希望在左侧对我的变量进行分组。
library(tidyverse)
library(ggalluvial)
data <- tibble(left = c("a","b", "c", "c", "d", "d"),
right = c("e", "e", "e", "f", "e", "f"),
values = c(1,2,3,2,3,2),
group = c("Group 3", "Group 2", "Group 1", "Group 1", "Group 2", "Group 2"))
ggplot(data,
aes(y = values, axis1 = left, axis2 = right)) +
geom_alluvium(aes(fill = group), width = 1/12) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_text(stat = "stratum", infer.label = TRUE,
nudge_x = -.1, fontface = "bold") +
scale_fill_brewer(type = "qual", palette = "Set1")这将生成下图:

我已经根据他们的组对流进行了着色。但我想根据他们的组对左侧进行分组。即新的顺序应该是从上到下的c,b,d,a,而不是默认的字母顺序。
我将非常感谢您帮助我找到解决方案。
谢谢。
发布于 2020-05-07 06:06:51
您需要将left定义为具有所需订单c("c", "b", "d", "a")中的级别的因子。
data$left <- factor(data$left, levels=c("c", "b", "d", "a"))
ggplot(data,
aes(y = values, axis1 = left, axis2 = right)) +
geom_alluvium(aes(fill = group), width = 1/12) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_text(stat = "stratum", infer.label = TRUE,
nudge_x = -.1, fontface = "bold") +
scale_fill_brewer(type = "qual", palette = "Set1")

https://stackoverflow.com/questions/61640589
复制相似问题