gpt4 book ai didi

r - 如何使用 R networkD3 值和每个节点下方的百分比绘制桑基图

转载 作者:行者123 更新时间:2023-12-02 01:35:50 28 4
gpt4 key购买 nike

下午好,从下面的代码我可以生成一个图表,但它不显示基础值。我尝试调整这个 thread 中的代码,但我没有得到任何乐趣。我从未使用过 Java。

我需要的是一个图表,其中还包含每个节点下的值和百分比,如下图所示。

enter image description here

谢谢

library(dplyr)
library(networkD3)
library(tidyverse)
library(readxl)
library(RColorBrewer)

df = data.frame(Source = c("ABC","CDE","MNB","PCI","UCD"),
Destination = c("Me","You","Him","Her","Her"),
Value = c(200,350,456,450,100))


## Reshape dataframe to long
df2 = pivot_longer(df, c(Destination, Source))

## make unique list for destination and source
dest = unique(as.character(df$Destination))
sources = unique(as.character(df$Source))

## Assign nodes number to each element of the chart
nodes2 = data.frame(node = append(dest,sources), nodeid = c(0:8))
res = merge(df,nodes2, by.x="Source", by.y = "node")
res = merge(res,nodes2, by.x="Destination", by.y = "node")

## Make links
links2 = res[, c("nodeid.x","nodeid.y","Value")]
colnames(links2) <- c("source", "target", "value")

## Add a 'group' column to each connection:
links2$group = as.factor(c("type_a","type_b","type_c","type_d","type_e"))


## defining nodes
nodes2["groups"] = nodes2$node
nodes2$groups = as.factor(nodes2$groups)



# Give a color for each group:
my_color <- 'd3.scaleOrdinal() .domain(["type_a","type_b","type_c","type_d","type_e","Me","You","Him","Her","Her"]) .range(["rgb(165,0,38,0.4)", "rgb(215,48,39, 0.4)", "rgb(244,109,67,0.4)", "rgb(253,174,97,0.4)", "rgb(254,224,139,0.4)",
"rgb(255,255,191,0.4)", "rgb(217,239,139,0.4)", "rgb(166,217,106,0.4)",
"rgb(102,189,99,0.4)","rgb(26,152,80,0.4)"])'


# plot graph
networkD3::sankeyNetwork(Links = links2, Nodes = nodes2,
Source = 'source',
Target = 'target',
Value = 'value',
NodeID = 'node',
units = 'Amount',
colourScale=my_color,
LinkGroup="group",
NodeGroup="groups",
fontFamily = "arial",
fontSize = 8,
nodeWidth = 8)

最佳答案

Update below original content; it is a fully developed solution to your original request.

我仍在努力用多行(而不是一行)渲染字符串。然而,事实证明它作为 SVG 文本相当困难。但是,这里有一种方法,您可以在图表中获取所有所需的信息,即使它的样式不完全按照您的意愿。

首先,我创建了要添加到绘图中的数据。必须在创建后将其添加到小部件中。 (如果您尝试事先添加它,它只会被删除。)

这将创建之前和之后的百分比以及汇总总和(如果需要)。

# for this tiny data frame some of this grouping is redundant---
# however, this method could be used on a much larger scale
df3 <- df %>%
group_by(Source) %>%
mutate(sPerc = paste0(round(sum(Value) / sum(df$Value) * 100, 2), "%")) %>%
group_by(Destination) %>%
mutate(dPerc = paste0(round(sum(Value) / sum(df$Value) * 100, 2), "%")) %>%
pivot_longer(c(Destination, Source)) %>%
mutate(Perc = ifelse(name == "Destination",
dPerc, sPerc)) %>% # determine which % to retain
select(Value, value, Perc) %>% # only fields to add to widget
group_by(value, Perc) %>%
summarise(Value = sum(Value)) # get the sum for 'Her'

我使用对象名称 plt 保存了桑基图。下一部分将新数据添加到小部件 plt

plt$x$nodes <- right_join(plt$x$nodes, df3, by = c("name" = "value"))

最后一个元素将值和百分比添加到源节点标签和目标节点标签。

htmlwidgets::onRender(plt, '
function(el, x) {
d3.select(el).selectAll(".node text")
.text(d => d.name + " " + d.Perc + " " + d.Value)
}')

enter image description here



更新:多行标签

我想我只是需要 sleep 。此更新将为您提供多行文本。

您还要求提供有关如何自己完成此操作的资源。这里有一些东西在起作用:Javascript、SVG 文本、D3 和 htmlwidgets 包。使用 onRender 时,了解将包 R 代码连接到包 htmlwidgets 的脚本文件非常重要。我建议从学习 htmlwidgets 开始。例如,如何创建您自己的。

好吧——回到回答原来的问题。这将使用我最初提供的所有内容附加新值,除了对 onRender 的调用。

htmlwidgets::onRender(plt, '
function(el, x) {
d3.select(el).selectAll(".node text").each(function(d){
var arr, val, anc
arr = " " + d.Perc + " " + d.Value;
arr = arr.split(" ");
val = d3.select(this).attr("x");
anc = d3.select(this).attr("text-anchor");
for(i = 0; i < arr.length; i++) {
d3.select(this).append("tspan")
.text(arr[i])
.attr("dy", i ? "1.2em" : 0)
.attr("x", val)
.attr("text-anchor", anc)
.attr("class", "tspan" + i);
}
})
}')

enter image description here

关于r - 如何使用 R networkD3 值和每个节点下方的百分比绘制桑基图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72404933/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com