简体   繁体   English

如何在R googleVis sankey图表中更改节点和链接颜色

[英]How to change node and link colors in R googleVis sankey chart

How can node and link colors be changed in R googleVis sankey chart? 如何在R googleVis sankey图表中更改节点和链接颜色? And link having the same color as its originating node? 并且链接与其始发节点具有相同的颜色?

library(googleVis)
datSK <- data.frame(From=c(rep("A",3), rep("B", 3)),
                To=c(rep(c("X", "Y", "Z"),2)),
                Weight=c(5,7,6,2,9,4))

Sankey <- gvisSankey(datSK, from="From", to="To", weight="Weight",
                 options=list(
                   sankey="{link: {color: { fill: '#d799ae' } },
                        node: { color: { fill: '#a61d4c' },
                        label: { color: '#871b47' } }}"))
plot(Sankey)

As soon as you have to color links from 2 originated nodes you'll need 2 colors for links. 只要您必须为来自2个始发节点的链接着色,您就需要2种颜色的链接。 Also you have 5 nodes in total, so you'll need 5 colors for them. 你总共有5个节点,所以你需要5种颜色。

Lets create 2 arrays in JSON format with colors for nodes and links 让我们用JSON格式创建2个数组,其中包含节点和链接的颜色

colors_link <- c('green', 'blue')
colors_link_array <- paste0("[", paste0("'", colors_link,"'", collapse = ','), "]")

colors_node <- c('yellow', 'lightblue', 'red', 'black', 'brown')
colors_node_array <- paste0("[", paste0("'", colors_node,"'", collapse = ','), "]")

Next, insert that array into options: 接下来,将该数组插入选项:

opts <- paste0("{
        link: { colorMode: 'source',
                colors: ", colors_link_array ," },
        node: { colors: ", colors_node_array ," }
      }" )

And, finally plot graph: 最后,情节图:

plot( gvisSankey(datSK, from="From", to="To", weight="Weight",
                     options=list(
                       sankey=opts)))

在此输入图像描述

Note, that in options colorMode is set to 'source' which means you would like to color links from originated nodes. 请注意,在选项中,colorMode设置为“source”,这意味着您要为来自原始节点的链接着色。 Alternatively, set 'target' to color links for destinated nodes 或者,将“target”设置为指定节点的颜色链接

EDIT: add description for multilevel sankeys 编辑:添加多级sankeys的说明

It is a bit tricky to find how to assign colors for multilevel sankeys. 找到如何为多级sankeys分配颜色有点棘手。

We need to create other dateframe: 我们需要创建其他日期框架:

datSK <- data.frame(From=c(rep("A",3), rep("B", 3), rep(c("X", "Y", "Z"), 2 )),
                To=c(rep(c("X", "Y", "Z"),2), rep("M", 3), rep("N", 3)),
                Weight=c(5,7,6,2,9,4,3,4,5,6, 4,8))

Here we have to change only arrays of colors. 这里我们只需要改变颜色数组。 Command to built plot is the same Let's assume we want these colors for the nodes and links : 建立绘图的命令是相同的让我们假设我们想要节点和链接的这些颜色:

colors_link <- c('green', 'blue', 'yellow', 'brown', 'red')
colors_link_array <- paste0("[", paste0("'", colors_link,"'", collapse = ','), "]")

colors_node <- c('yellow', 'lightblue', 'red', 'black', 'brown', 'green', 'brown')
colors_node_array <- paste0("[", paste0("'", colors_node,"'", collapse = ','), "]")

Result would be : 结果将是:

在此输入图像描述

The most trickiest part is to understand how these colors are assigned: 最棘手的部分是了解如何分配这些颜色:

  1. Links are assigned in the order they appear in dataset (row_wise) 链接按照它们在数据集中出现的顺序分配(row_wise)

在此输入图像描述

  1. For the nodes colors are assigned in the order plot is built. 对于节点颜色是在订单图中分配的。

    • From A to X, Y, Z - green 从A到X,Y,Z - 绿色
    • From X to M, N - blue 从X到M,N - 蓝色
    • From Y to M, N - yellow 从Y到M,N - 黄色
    • From Z to M, N - brown 从Z到M,N - 棕色
    • From B to X, Y, Z - red 从B到X,Y,Z - 红色

More detailed information on how to format sankey diagram : https://developers.google.com/chart/interactive/docs/gallery/sankey 有关如何格式化sankey图表的更多详细信息: https//developers.google.com/chart/interactive/docs/gallery/sankey

I know this is older but in case anyone else is ever stuck on this - I figured out how to make the proper order and generate a string of color nodes so you can have custom colors for certain labels. 我知道这个更老了,但万一其他人都坚持这个 - 我想出了如何制作正确的顺序并生成一串颜色节点,这样你就可以为某些标签定制颜色。 Shout out to @vadym-b for the data and explaining about the order. 向@ vadym-b喊出数据并解释订单。 Check it out: 看看这个:

#convert to list combined of source and target for color order
# edges is a dataframe from @vadym-b's answer above
edges <- data.frame(From=c(rep("A",3), rep("B", 3), rep(c("X", "Y", "Z"), 2 )),
                    To=c(rep(c("X", "Y", "Z"),2), rep("M", 3), rep("N", 3)),
                    Weight=c(5,7,6,2,9,4,3,4,5,6, 4,8))

#we have to make the order right - you need a list
# that is a combination of From, To, From, To, From, To
nc.df <- c()
for (i in 1:nrow(edges)) {
  nc.df <- c(nc.df, as.character(edges$From[i]), as.character(edges$To[i]))
}

#the actualy parsing - get the unique list and return
# colors based on what the source or target value is
nodeColor <- sapply(unique(nc.df), function(r) {
  if (grepl('A',r)) return('red')
  if (grepl('B',r)) return('red')
  if (grepl('Z',r)) return('green')
  if (grepl('X',r)) return('green')
  if (grepl('Y',r)) return('purple')
  if (grepl('M',r)) return('blue')
  if (grepl('N',r)) return('blue')
  #return a default color if you like
  return('black')
})

#make a sankey
library(googleVis)

# put the color list in a collapsed string
sankey <- gvisSankey(
  edges, 
  chartid = 'Sankey', 
  from="From", 
  to="To", 
  weight="Weight", 
  options=list(
    sankey = paste0("{
      iterations: 0,
      link: {
        colorMode: 'gradient'
      },
      node: {
        colors: ['",paste(nodeColor,collapse="','"),"']
      }
    }")
  )
)

plot(sankey)

Sankey定制颜色

I have put on github a piece of code that does that. 我已经在github上添加了一段代码。

#TOPLOTS[,1] = from ; TOPLOTS[,1] = to
names_pahtwayorder<-unlist(data.frame(t(TOPLOTs[,1:2])))
names_pahtwayorder<-names_pahtwayorder[!duplicated(names_pahtwayorder)]
names(names_pahtwayorder)<-NULL; names_pahtwayorder

https://github.com/SkanderMulder/ExtractIPA/blob/master/functionSankey.r https://github.com/SkanderMulder/ExtractIPA/blob/master/functionSankey.r

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM