'Sankey diagram in R; replacing log-scale value with normal value in "hover-box"
I am creating some sankey diagrams in R which shows the relationships between antecedent and consequent events, with the frequency of the relationships being plotted on the sankey diagram. I have been trying to make the results a bit more interpretable (I had asked a question before about this:- Creating Sankey diagram in R; making the plot output interpretable)
Here is an example of what I have so far, with mock data and output:-
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
which gives this:-
While it's going in the right direction, it does not show all the results and I would like to tidy up the size of the entities.
So what I do is I log-scale the count variable.
dfsankey$count<-log10(dfsankey$count)
And replot, which gives it a tidier look:-
However, the value inside the "hover-box" is the log-scaled value i.e. log10(1694888)=6.23
My aim is to replace this log-scaled value with the original value, but maintain the new plot look. Is there a way that I can paste the original value into the hover-box?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


