admin管理员组文章数量:1304224
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)
Share
Improve this question
asked Feb 4 at 12:19
PatrickPatrick
1,45312 silver badges24 bronze badges
1 Answer
Reset to default 1We 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))
本文标签: rHow can I prevent overlapping labels in googleVisgvisSankey()Stack Overflow
版权声明:本文标题:r - How can I prevent overlapping labels in googleVis::gvisSankey()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741767305a2396538.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论