【问题标题】:Is there an option to keep the same color for nodes with the same label? Sankey plot in R是否可以选择为具有相同标签的节点保持相同的颜色? R中的桑基图
【发布时间】:2019-08-15 08:07:33
【问题描述】:

我正在创建具有 4 年观察值的 Sankey 图,每年我都有相同的变量(节点与前一年具有相同的标签9。我希望每个具有相同标签的节点具有相同的颜色。这是可以吗?

我尝试了一些选项,但没有找到解决方案。

options = list(sankey="{
    link: { colorMode: 'source'},

    node: {colorMode:'string',  colors: ['#1f78b4','#a6cee3', ]}}"), )

R 代码:

Sankey <- gvisSankey(SHIVALVCI_widefile_1016_sankey, from="Var1", to="Var2", weight="Freq",

options = list(sankey="{
    link: { colorMode: 'source'},

    node: {colorMode:'string',  colors: ['#1f78b4','#a6cee3', ]}}"), )

以及标签的名称:

"IK","No prest"))
SHIVALVCI_widefile_1016$M48 <- factor(SHIVALVCI_widefile_1016$M48,
    levels = c(1111,1112,1121,1122,1211,1212,1221,1222,2111,2112,2121,2122,2211,2212,2221,2222),
    labels = c("All prest","IV ALV SH","IV ALV IK","IV ALV","IV SH IK","IV SH",
    "IV IK","IV","ALV SH IK","ALV SH","ALV IK","ALV","SH IK","SH",
    "IK","No prest"))

【问题讨论】:

    标签: r colors sankey-diagram googlevis


    【解决方案1】:

    您应该能够使用ggforce 相对轻松地执行此类操作。以下似乎与泰坦尼克号数据有关。我从?geom_parallel_sets 中的示例稍微修改了代码,将fill 美学添加到geom_parallel_sets_axes(),以便颜色映射到X 轴变量的级别(在您的情况下为年份)。请注意,由于节点不会在此处重复(就像您在不同年份一样),因此 X 轴上每个变量的颜色都是唯一的。

    library(ggforce)
    ?geom_parallel_sets
    
    
    data <- reshape2::melt(Titanic)
    data <- gather_set_data(data, 1:4)
    ggplot(data, aes(x, id = id, split = y, value = value)) +
      geom_parallel_sets(aes(fill = Sex), alpha = 0.3, axis.width = 0.1) +
      geom_parallel_sets_axes(aes(fill=y), axis.width = 0.1) +
      geom_parallel_sets_labels(colour = 'white')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 2020-01-14
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多