Я использую пакет R networkD3
для построения следующей диаграммы Санки. Значения устанавливают размер ссылок. Мне нужна функция, которая добавляет то же значение, что и текст, внутри узлов.
# Library
library(networkD3)
library(dplyr)
# Make a connection data frame
links <- data.frame(
source=c("group_A","group_A", "group_B", "group_C", "group_C", "group_E"),
target=c("group_C","group_D", "group_E", "group_F", "group_G", "group_H"),
value=c(2,3, 2, 3, 1, 3)
)
# From these flows we need to create a node data frame: it lists every entities involved in the flow
nodes <- data.frame(
name=c(as.character(links$source), as.character(links$target)) %>%
unique()
)
# With networkD3, connection must be provided using id, not using real name like in the links dataframe.. So we need to reformat it.
links$IDsource <- match(links$source, nodes$name)-1
links$IDtarget <- match(links$target, nodes$name)-1
# Add a 'group' column to each connection:
links$group <- as.factor(c("type_a","type_a","type_a","type_b","type_b","type_b"))
# Add a 'group' column to each node. Here I decide to put all of them in the same group to make them grey
nodes$group <- as.factor(c("my_unique_group"))
# Give a color for each group:
my_color <- 'd3.scaleOrdinal() .domain(["type_a", "type_b", "my_unique_group"]) .range(["#69b3a2", "steelblue", "grey"])'
# Make the Network
p <- sankeyNetwork(Links = links, Nodes = nodes, Source = "IDsource", Target = "IDtarget",
Value = "value", NodeID = "name",
colourScale=my_color, LinkGroup="group", NodeGroup="group")
p
В приведенном выше коде показана структура кода диаграммы Санки, описанная также в следующей ссылке: https://www.r-graph-gallery.com/322-custom-colours-in-sankey-diagram.html
ggalluvial
? Затем используйтеtheme_void()
, чтобы получить диаграмму Санки (без учета частоты на осиy
) - person csgroen   schedule 13.08.2019R ...source=c("group_A" + "value [1]","group_A"+"value[2]", ...), ...
- person JessicaRjes   schedule 13.08.2019links$source <- paste(links$source, "-", links$value)
, но это не совсем сработает, потому что то, что вы видите на графике, - это метка узлов, а не метка ссылок. Имейте в виду, что один узел может иметь более одной ссылки, выходящей / входящей в него, поэтому значение узла должно быть суммой значений ссылок, которые выходят или входят в него. - person CJ Yetman   schedule 13.08.2019