我正在制作一個桑基圖ggalluvial。
這是我的資料集
library(ggsankey)
library(tidyverse)
df <-
mtcars %>%
make_long(cyl, vs, am, gear, carb) %>%
mutate(color = c(rep("red", 80), rep("blue", 80)))
您可以獲得像這樣的桑基圖:
df %>%
ggplot(aes(x = x,
next_x = next_x,
node = node,
next_node = next_node,
fill = factor(node),
label = factor(node)))
geom_sankey()
geom_sankey(flow.alpha = .6,
node.color = "gray30")
geom_sankey_label(size = 3, color = "white", fill = "gray40")
scale_fill_viridis_d()
theme_sankey(base_size = 18)
labs(x = NULL)
theme(legend.position = "none",
plot.title = element_text(hjust = .5))

現在,我想在列顏色標簽之間流動color的df。是否可以?如果沒有,你知道在 R 中還有其他方法嗎?
我試過:
df %>%
ggplot(aes(x = x,
next_x = next_x,
node = node,
next_node = next_node,
fill = factor(color),
label = factor(node)))
geom_sankey()
geom_sankey(flow.alpha = .6,
node.color = "gray30")
geom_sankey_label(size = 3, color = "white", fill = "gray40")
scale_fill_viridis_d()
theme_sankey(base_size = 18)
labs(x = NULL)
theme(legend.position = "none",
plot.title = element_text(hjust = .5))
但情節似乎完全破裂:

uj5u.com熱心網友回復:
最后,ggaluvial似乎更適合我的問題:
下面是資料格式:
df <-
mtcars %>%
select(cyl, vs, am, gear, carb) %>%
mutate(color = c(rep("red", nrow(mtcars)/2), rep("blue", nrow(mtcars)/2)),
id = seq(1:nrow(mtcars))) %>%
pivot_longer(cols = !c(color, id),
names_to = "var",
values_to = "state")
這是具有正確流顏色的圖:
df %>%
ggplot(aes(x = var,
stratum = state,
label = state,
alluvium = id))
stat_alluvium(aes(fill = color),
width = 0,
alpha = 1,
geom = "flow")
geom_stratum(width = 0.2)
geom_text(stat = "stratum", size = 5, angle = 90)
theme_bw()

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