Under certain circumstances, categories in googleVis::gvisSankey()
output will align in unfortunate ways. As visible from the example below, it is basically impossible to properly read the labels. Is there a generic solution or at least a workaround for this?
library(googleVis) # 0.7.1
dat <- data.frame(From = c("A", "B", "C",
"LONG LONG LONG X",
"LONG LONG LONG Y",
"LONG LONG LONG Z"),
To = c("LONG LONG LONG X",
"LONG LONG LONG Y",
"LONG LONG LONG Z",
"LONG LONG LONG 1",
"LONG LONG LONG 2",
"LONG LONG LONG 3"),
Weight = rep(5, 6))
sk <- gvisSankey(dat, from = "From", to = "To", weight = "Weight")
plot(sk)
Under certain circumstances, categories in googleVis::gvisSankey()
output will align in unfortunate ways. As visible from the example below, it is basically impossible to properly read the labels. Is there a generic solution or at least a workaround for this?
library(googleVis) # 0.7.1
dat <- data.frame(From = c("A", "B", "C",
"LONG LONG LONG X",
"LONG LONG LONG Y",
"LONG LONG LONG Z"),
To = c("LONG LONG LONG X",
"LONG LONG LONG Y",
"LONG LONG LONG Z",
"LONG LONG LONG 1",
"LONG LONG LONG 2",
"LONG LONG LONG 3"),
Weight = rep(5, 6))
sk <- gvisSankey(dat, from = "From", to = "To", weight = "Weight")
plot(sk)
We can insert some custom JavaScript to mingle with the text elements. However, this is only for show and stays so long you don't use your mouse to hover over the sankey!
#install.packages("googleVis")
library(googleVis) # 0.7.1
dat <- data.frame(From = c("A", "B", "C",
"LONG LONG LONG X",
"LONG LONG LONG Y",
"LONG LONG LONG Z"),
To = c("LONG LONG LONG X",
"LONG LONG LONG Y",
"LONG LONG LONG Z",
"LONG LONG LONG 1",
"LONG LONG LONG 2",
"LONG LONG LONG 3"),
Weight = rep(5, 6))
sk <- gvisSankey(dat, from="From", to="To", weight="Weight")
sk$html$chart[["jsDrawChart"]] <- paste0(sub("}\\s*$", "", sk$html$chart[["jsDrawChart"]]),
"
var svg = document.querySelector('svg');
if (!svg) {
console.log('SVG not found!');
return;
}
var texts = svg.querySelectorAll('text');
var seenY = {}; // Object to store encountered y positions and counts
texts.forEach(function(text) {
var y = parseFloat(text.getAttribute('y'));
console.log('Original y:', y);
if (seenY[y] === undefined) {
seenY[y] = 0; // First time encountering this y position
} else {
seenY[y]++; // Increment counter for duplicates
var newY = y + seenY[y] * 20; // Shift text down by 10px for each duplicate
console.log(`Moving text from ${y} to ${newY}`);
text.setAttribute('y', newY);
}
});
};"
)
plot(sk)
Or better yet, set height
& width
to circumvent the problem.
sk <- gvisSankey(dat, from="From", to="To", weight="Weight",
options=list(
height=600,
width=800))