Plotly/Vistime - 重叠标签 [英] Plotly/Vistime - overlapping labels
问题描述
我有一个使用 vistime/plotly 构建的简单时间线:
I have a simple timeline I've built using vistime/plotly:
library(vistime)
library(plotly)
data <- read.csv(text="event,group,start,end,color
Bought Apples,Shopping,2020-06-15,2020-10-15,#fed8b1
Shoes,Shopping,2020-07-01,2020-07-01,#fed8b1
Shoes,Shopping,2020-07-01,2020-07-01,#fed8b1
Shoes,Shopping,2020-07-02,2020-07-02,#fed8b1
Shirts,Shopping,2020-07-01,2020-07-01,#fed8b1
Invoice,Charity,2020-08-30,2020-08-30,#fed8b1
Report 1,Work,2020-06-10,2020-06-10,#ffcccb
Meeting 5,Meetings,2020-08-15,2020-08-15,#b19cd9 ")
p <- vistime(data)
pb <- plotly_build(p)
#change label size
for (i in 1:length(pb$x$data)) {
if (pb$x$data[[i]]$mode == "text") pb$x$data[[i]]$textfont$size <- 10
}
#change x axis font size:
#pb$x$layout$xaxis$tickfont <- list(size = 28)
# change marker size
for (i in 1:length(pb$x$data)) {
if (pb$x$data[[i]]$mode == "markers") pb$x$data[[i]]$marker$size <- 20
}
pb
这给了我:
如您所见,如果有多个点聚集在一起,标签会发生冲突.有什么相当简单的方法可以避免这个问题吗?一个不太复杂的开箱即用的解决方案?我想知道在 vistime 或 plotly 中是否有任何内置方法来处理这个问题.谢谢!
As you can see, if there are multiple points clustered close together, the labels collide. Is there any fairly easy way to avoid this problem? An out-of-the box solution that isn't too complex? I'm wondering if there's any built in methods in vistime or plotly to deal with this. Thanks!
推荐答案
不幸的是,Plotly 无法避免这些重叠的标签(或者我可能只是不知道).但是,使用 gg_vistime()
和 hc_vistime()
会自动避免这些重叠:
Unfortunately, Plotly has no option to avoid these overlapping labels (or maybe I just don't know about one). However, using gg_vistime()
and hc_vistime()
these overlaps are avoided automatically:
gg_vistime(data)
hc_vistime(data)
文档:
https://cran.r-project.org/web/packages/vistime/vignettes/gg_vistime-vignette.html
https://cran.r-project.org/web/packages/vistime/vignettes/hc_vistime-vignette.html
免责声明:我写了这个包
Disclaimer: I wrote this package
这篇关于Plotly/Vistime - 重叠标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!