R语言 带ggplot的桑基图

vq8itlhq  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(237)

我在透视表方面比较有经验,但是这个几乎让我发疯。我想生成一个Sankey图,其中包含连接两个实验之间参数的流。这非常简单,但我不知道软件包希望数据如何布局。
请考虑以下MWE:

tribble(
~parm, ~value,
"b1", 0.009,
"g1", 0.664,
"b2", 0.000,
"ra", 0.000,
"rc", 0.000,
"ax", 0.084,
"cx", 0.086,
"ex", 0.179,
"ay", 0.045,
"cy", 0.043,
"ey", 0.102
) -> doc1

doc2 <- tribble(
  ~parm, ~value,
  "b1", 0.181,
  "g1", 0.289,
  "b2", 0.181,
  "ra", 0.000,
  "rc", 0.000,
  "ax", 0.001,
  "cx", 0.001,
  "ex", 0.002,
  "ay", 0.001,
  "cy", 0.001,
  "ey", 0.002,
  "re", 0.000,
  "rf", 0.000,
  "b3", 0.289
)

doc1 <- doc1 %>% mutate(model = "exp")
doc2 <- doc2 %>% mutate(model = "exp2")
finalpow <- doc1 %>% full_join(doc2)
a <- pivot_wider(finalpow, names_from = model, values_from = value)

finalpow  <- a%>% make_long( exp, exp2)

和下面的代码来生成关系图:

ggplot(finalpow, aes(x = x,
               next_x = next_x,
               node = node,
               next_node = next_node,
               fill = factor(node),
               label = node)) +
  geom_sankey(flow.alpha = 0.5, node.color = 1) +
  geom_sankey_label(size = 3.5, color = 1, fill = "white") +
  scale_fill_viridis_d() +
  theme_sankey(base_size = 16) +
  guides(fill = guide_legend(title = "Title"))

我很接近了,因为两个条形图是所需的实验,但标签应该是不同的参数名称ax、cx、ex ......,“条形图”应该与每个参数值成比例,下面是我得到的结果:

m3eecexj

m3eecexj1#

如果您可以使用其他包,我个人更喜欢{ggalluvial}包-我发现语法更直观,文档中的示例有助于更好地理解所需的数据结构。
我认为这里最大的挑战是标签--正如你在下面的代码中看到的,这占用了最大的代码块。也许你可能想避免在这种情况下直接使用标签。

library(tidyverse)
library(ggalluvial)

## using your data frame a
finalpow <- a %>% pivot_longer(cols = c(exp, exp2)) %>%
  ## you need to manually drop all NA, because geom_alluvium won't do that for you
  drop_na()
## I'd  create a separate data frame for annotation
df_lab <- finalpow %>%
  filter(name == "exp") %>%
  ## for labelling, you need to manually arrange labels in correct position
  arrange(desc(parm))

ggplot(mapping = aes(y = value, x = name)) +
  geom_alluvium(
    data = finalpow,
    aes(fill = parm, alluvium = parm),
    color = "black", alpha = 1
  ) +
  ggrepel::geom_label_repel(
    data = df_lab,
    aes(label = parm, y = value),
    ## you cannot just use the y value, you need to stack
    position = position_stack(vjust = .5)
  )

创建于2022年12月28日,使用reprex v2.0.2

相关问题