ggsankey:标签和节点在输出中的位置不同

问题描述 投票:0回答:1

为了实现没有 NA 节点的 sankey,我根据这篇文章中的建议调整了我的代码: 文字

在我的输出中,标签的数量和位置与相应的节点不匹配。

问题:

  • 如何解决这个问题以及为什么会发生这种情况? -- 节点和标签的位置(aes?)是单独计算的吗?
  • 一般来说:我如何检查和/或更改 x&y 值(aes?),例如geom_sankey_text() 还是 geom_sankey_label()?

提前致谢!

这是代码:

library(ggsankey)
library(ggplot2)
library(dplyr)

set.seed(42)
size<-20
df<-data.frame(id=1:size,
               Inclusion=sample(c("Group1", "Group2", "Group3"), size, replace=T),
               t1=sample(c("Drug1","Drug2", NA), size, replace=T, prob = c(0.3,0.5, 0.2)),
               t2=sample(c("otherTherapy1", "otherTherapy2", NA), size, replace=T, prob = c(0.2,0.3, 0.5)), 
               outcome=sample(c("Dead","Or", "Alive"), size, replace=T, prob = c(0.3,0.38,0.32)))

do.call(rbind, apply(df, 1, function(x) {
  x <- na.omit(x[-1])
  data.frame(x = names(x), node = x, 
             next_x = dplyr::lead(names(x)), 
             next_node = dplyr::lead(x), row.names = NULL)
})) %>%  mutate(x = factor(x, names(df)[-1]),
                next_x = factor(next_x, names(df)[-1])) %>%
  ggplot( aes(x = x,
              next_x = next_x,
              node = node,
              next_node = next_node,
              fill = node,
              label = node)) +
  geom_sankey(flow.alpha = 0.5,
              node.color = NA,
              show.legend = TRUE) +
  geom_sankey_text(size = 3, color = "black", fill = NA, hjust = 0, 
                   position = position_nudge(x = 0.1))+
  theme(legend.position = "none")

''' 这是输出: enter image description here

我在此处的档案和互联网上没有找到对此的解释。

r ggplot2 label nodes sankey-diagram
1个回答
0
投票

我也遇到了类似的问题。我发现在 geom_sankey_label 中包含“group”子句解决了这个问题!

例如,在您的代码中使用它应该可以工作

geom_sankey_text(aes(组=节点,标签=节点),大小= 3,颜色=“黑色”,填充= NA,hjust = 0,位置=position_nudge(x = 0.1))

© www.soinside.com 2019 - 2024. All rights reserved.