R 中的桑基图;用 "hover-box" 中的正常值替换对数刻度值

Sankey diagram in R; replacing log-scale value with normal value in "hover-box"

我正在用 R 创建一些桑基图,它显示了先行事件和后续事件之间的关系,并在桑基图上绘制了关系的频率。 我一直在努力使结果更具可解释性(我之前曾问过一个问题:- Creating Sankey diagram in R; making the plot output interpretable

这是我目前拥有的示例,包含模拟数据和输出:-

library(dplyr)
library(networkD3)

#df creation=====================================================

dfsankey <- tibble::tribble(
  ~Antecedent,  ~Consequent,   ~count,
  "Activity 1", "Activity 1", 1694888L,
  "Activity 1", "Activity 2",     170L,
  "Activity 1", "Activity 3",    4060L,
  "Activity 1", "Activity 4",       0L,
  "Activity 1", "Activity 5",       7L,
  "Activity 2", "Activity 1",     255L,
  "Activity 2", "Activity 2",   46564L,
  "Activity 2", "Activity 3",     756L,
  "Activity 2", "Activity 4",      38L,
  "Activity 2", "Activity 5",      43L,
  "Activity 3", "Activity 1",    3926L,
  "Activity 3", "Activity 2",     523L,
  "Activity 3", "Activity 3",  303979L,
  "Activity 3", "Activity 4",     689L,
  "Activity 3", "Activity 5",     711L,
  "Activity 4", "Activity 1",       0L,
  "Activity 4", "Activity 2",      51L,
  "Activity 4", "Activity 3",     670L,
  "Activity 4", "Activity 4",   35210L,
  "Activity 4", "Activity 5",     383L,
  "Activity 5", "Activity 1",      13L,
  "Activity 5", "Activity 2",      59L,
  "Activity 5", "Activity 3",     800L,
  "Activity 5", "Activity 4",     508L,
  "Activity 5", "Activity 5",   14246L
)

links <- dfsankey %>% 
  mutate(
    Antecedent = paste("Antecedent", Antecedent),
    Consequent = paste("Consequent", Consequent),
  )

# Create a data frame for nodes
nodes <- links %>% 
  summarise(name = union(Antecedent, Consequent))

# Find node IDs for links
links$IDsource <- match(links$Antecedent, nodes$name) - 1
links$IDtarget <- match(links$Consequent, nodes$name) - 1

sankeyNetwork(
  Links = links,
  Nodes = nodes,
  Source = "IDsource",
  Target = "IDtarget",
  Value = "count",
  NodeID = "name"
) -> p

p

这给出了这个:-

虽然方向正确,但并未显示所有结果,我想整理一下实体的大小。

所以我所做的是对 count 变量进行对数缩放。

dfsankey$count<-log10(dfsankey$count)

并重新绘图,使它看起来更整洁:-

但是,“悬停框”内的值是对数标度值,即 log10(1694888)=6.23

我的目标是用原始值替换这个对数标度值,但保持新的绘图外观。有什么方法可以将原始值粘贴到悬停框中吗?

我可能不建议像您那样记录值,但将其视为给定...

您可以将原始值添加回 sankeyNetwork() 生成的 中的数据,然后使用 htmlwidgets::onRender() 在小部件时注入一些 JavaScript加载以更改工具提示文本。

将计数转换为计数的对数时,不要覆盖原始变量...

dfsankey$log_count <- log10(dfsankey$count)

然后在创建 Sankey htmlwidget 之后,将原始计数值添加回...

p$x$links$count <- dfsankey$count

然后使用htmlwidgets::onRender()注入一些JavaScript...

htmlwidgets::onRender(p, '
  function(el) {
    d3.select(el).select("svg")
      .selectAll(".link")
      .select("title foreignObject body pre")
      .text(d => d.count);
  }
')

一起...

library(dplyr)
library(networkD3)
library(htmlwidgets)

#df creation=====================================================

dfsankey <- tibble::tribble(
  ~Antecedent,  ~Consequent,   ~count,
  "Activity 1", "Activity 1", 1694888L,
  "Activity 1", "Activity 2",     170L,
  "Activity 1", "Activity 3",    4060L,
  "Activity 1", "Activity 4",       0L,
  "Activity 1", "Activity 5",       7L,
  "Activity 2", "Activity 1",     255L,
  "Activity 2", "Activity 2",   46564L,
  "Activity 2", "Activity 3",     756L,
  "Activity 2", "Activity 4",      38L,
  "Activity 2", "Activity 5",      43L,
  "Activity 3", "Activity 1",    3926L,
  "Activity 3", "Activity 2",     523L,
  "Activity 3", "Activity 3",  303979L,
  "Activity 3", "Activity 4",     689L,
  "Activity 3", "Activity 5",     711L,
  "Activity 4", "Activity 1",       0L,
  "Activity 4", "Activity 2",      51L,
  "Activity 4", "Activity 3",     670L,
  "Activity 4", "Activity 4",   35210L,
  "Activity 4", "Activity 5",     383L,
  "Activity 5", "Activity 1",      13L,
  "Activity 5", "Activity 2",      59L,
  "Activity 5", "Activity 3",     800L,
  "Activity 5", "Activity 4",     508L,
  "Activity 5", "Activity 5",   14246L
)

dfsankey$log_count <- log10(dfsankey$count)

links <- dfsankey %>% 
  mutate(
    Antecedent = paste("Antecedent", Antecedent),
    Consequent = paste("Consequent", Consequent),
  )

# Create a data frame for nodes
nodes <- links %>% 
  summarise(name = union(Antecedent, Consequent))

# Find node IDs for links
links$IDsource <- match(links$Antecedent, nodes$name) - 1
links$IDtarget <- match(links$Consequent, nodes$name) - 1

sankeyNetwork(
  Links = links,
  Nodes = nodes,
  Source = "IDsource",
  Target = "IDtarget",
  Value = "log_count",
  NodeID = "name"
) -> p

p$x$links$count <- dfsankey$count

htmlwidgets::onRender(p, '
  function(el) {
    d3.select(el).select("svg")
      .selectAll(".link")
      .select("title foreignObject body pre")
      .text(d => d.count);
  }
')