如何在R googleVis sankey图表中更改节点和链接颜色?并且链接与其始发节点具有相同的颜色?
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)
答案 0 :(得分:9)
只要您必须为来自2个始发节点的链接着色,您就需要2种颜色的链接。 你总共有5个节点,所以你需要5种颜色。
让我们用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 = ','), "]")
接下来,将该数组插入选项:
opts <- paste0("{
link: { colorMode: 'source',
colors: ", colors_link_array ," },
node: { colors: ", colors_node_array ," }
}" )
最后,情节图:
plot( gvisSankey(datSK, from="From", to="To", weight="Weight",
options=list(
sankey=opts)))
请注意,在选项中,colorMode设置为“source”,这意味着您希望为来自原始节点的链接着色。或者,将“target”设置为指定节点的颜色链接
编辑:添加多级桑克的说明
找到如何为多级sankeys分配颜色有点棘手。
我们需要创建其他日期框架:
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))
这里我们只需要改变颜色数组。建立情节的命令是一样的 让我们假设我们想要节点和链接的这些颜色:
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 = ','), "]")
结果将是:
最棘手的部分是了解如何分配这些颜色:
对于节点颜色是在订单图中指定的。
有关如何格式化sankey图表的更多详细信息:https://developers.google.com/chart/interactive/docs/gallery/sankey
答案 1 :(得分:2)
我知道这比较老,但万一其他人陷入困境,我想出了如何进行正确排序并生成一串颜色节点的方法,以便可以为某些标签使用自定义颜色。向@ vadym-b喊出数据并解释顺序。检查一下:
#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)
答案 2 :(得分:1)
我已经在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