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

ggsankey: Labels and nodes do not have same position in the output

提问人:dtw 提问时间:11/14/2023 最后编辑:Peterdtw 更新时间:11/14/2023 访问量:38

问:

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

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

问题:

  • 如何解决这个问题,为什么会发生这种情况? -- 节点的位置 (AE?) 和标签是单独计算的吗?
  • 一般来说:我如何检查和/或更改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 标签 节点 sankey-diagram

评论


答: 暂无答案