简体   繁体   English

忽略 hoverinfo = 'skip'

[英]Plotly ignoring hoverinfo = 'skip'

I am creating a plot with several time series, plotting the mean value and two quantiles in the process and highlighting the region between the quantiles.我正在创建一个包含多个时间序列的图,绘制过程中的平均值和两个分位数并突出显示分位数之间的区域。 Then I have a unified hoverinfo showing me all points at one point in time.然后我有一个统一的悬停信息显示我在一个时间点的所有点。 I do not want this hoverinfo to display the dummy trace that is only there for the highlighting.我不希望这个 hoverinfo 显示仅用于突出显示的虚拟跟踪。 I tried both hoverinfo = skip and hoverinfo = none , but that only hides any text info, not the trace itself in the hoverinfo.我尝试了hoverinfo = skiphoverinfo = none ,但这只会隐藏任何文本信息,而不是 hoverinfo 中的跟踪本身。

Here is the code I have (without most "beautification parameters" such as axis width and the likes...):这是我拥有的代码(没有大多数“美化参数”,例如轴宽等......):

###creating dummy data
test_data <- data.frame(matrix(0,nrow = 27,ncol = 4))
colnames(test_data) <- c("x","y","value","date")
test_data$x <- c(-1,-1,-1,0,0,0,1,1,1,-1,-1,-1,0,0,0,1,1,1,-1,-1,-1,0,0,0,1,1,1)
test_data$y <- c(-1,0,1,-1,0,1,-1,0,1,-1,0,1,-1,0,1,-1,0,1,-1,0,1,-1,0,1,-1,0,1)
test_data$value <- rnorm(27,mean = 5,sd = 1)

date_vec <- c(rep(as.Date("2022-01-01"),9),rep(as.Date("2022-01-02"),9),rep(as.Date("2022-01-03"),9))

test_data$date <- date_vec

x_levels <- unique(test_data$x)
y_levels <- unique(test_data$y)

avg <- data.frame(matrix(0,nrow = 3,ncol = 1))
colnames(avg) <- c("value")
avg$date <- unique(date_vec)
Q75 <- data.frame(matrix(0,nrow = 3,ncol = 1))
colnames(Q75) <- c("value")
Q75$date <- unique(date_vec)
Q25 <- data.frame(matrix(0,nrow = 3,ncol = 1))
colnames(Q25) <- c("value")
Q25$date <- unique(date_vec)

for (i in 1:length(unique(date_vec))){
  avg$value[i] <- mean(test_data$value[test_data$date == unique(date_vec)[i]])
  Q75$value[i] <- quantile(test_data$value[test_data$date == unique(date_vec)[i]],0.75)
  Q25$value[i] <- quantile(test_data$value[test_data$date == unique(date_vec)[i]],0.25)
}

##creating plot
fig <- plot_ly()
fig <- fig %>% layout(hovermode = 'x unified')

for(i in 1:length(x_levels)){
for(j in 1:length(y_levels)){
fig <- fig %>% add_trace(data = test_data, type = 'scatter', mode = 'lines+markers',
                         x = test_data$date[test_data$x == x_levels[i] & test_data$y == y_levels[j]],
                         y = test_data$value[test_data$x == x_levels[i] & test_data$y == y_levels[j]],
                         showlegend = FALSE, line = list(color = 'grey'), marker = list(color = 'grey'), 
                         hoverinfo = 'text', text = paste0("some text dependent on x and y"))
}}

fig <- fig %>% add_trace(data = Q25, type = 'scatter', mode = 'lines', name = '25%-quantile', x = Q25$date,
                         y = Q25$value, showlegend = TRUE, line = list(color = 'darkred', dash = 'dot'),
                         hoverinfo = 'text', text = paste0("some text dependent on Q25"))

fig <- fig %>% add_trace(data = avg, type = 'scatter', mode = 'lines', name = 'mean value', x = avg$date,
                         y = avg$value, showlegend = TRUE, line = list(color = 'darkred', dahs = 'dash'),
                         hoverinfo = 'text', text = paste0("some text depenent on avg"))

fig <- fig %>% add_trace(data = Q75, type = 'scatter', mode = 'lines', name = '75%-quantile', x = Q75$date,
                         y = Q75$value, showlegend = TRUE, line = list(color = 'darkred', dash = 'dot'),
                         hoverinfo = 'text', text = paste0("some text dependent on Q75"))

#### This is the dummy trace in question:
fig <- fig %>% add_trace(x = Q25$date, y = Q25$value, type = 'scatter', mode = 'lines', fill = 'tonexty',
                         fillcolor = 'rgba(139,0,0,0.2)', line = list(color = 'darkred', width = 0.1), showlegend = FALSE,
                         hoverinfo = 'skip')

print(fig)

Is this a bug?这是一个错误吗? Am I missing something obvious?我错过了一些明显的东西吗? Is there a better way to make the highlighting that does not create an additional hoverinfo?有没有更好的方法来突出显示不会创建额外的悬停信息? Thanks in advance!提前致谢!

Alright, this answer is not ideal—but it works.好吧,这个答案并不理想——但它有效。

There is good news, though.不过,有好消息。 Plotly developers are already working on a solution. Plotly 开发人员已经在研究解决方案。 They're in the testing phase, which is where I got the idea for this answer.他们正处于测试阶段,这就是我得到这个答案的想法的地方。 You can read about what Plotly's doing here.你可以在这里阅读 Plotly 正在做的事情。

First, I set a seed because you have random data.首先,我设置了一个种子,因为你有随机数据。 I used set.seed(3509) .我使用set.seed(3509) There were no changes to the data or fig .数据或fig没有变化。

When you hover over any point on your graph, your fill color will disappear.当您将鼠标悬停在图表上的任何点上时,您的填充颜色将消失。 The tooltip does not show the trace that shouldn't be there anyway.工具提示不会显示无论如何都不应该存在的跟踪。

The fill color will return as soon as you move on or when the tooltip goes away.当您继续前进或工具提示消失时,填充颜色将立即返回。

This is the Javascript to call this hover event.这是调用此悬停事件的 Javascript。

# the notation [12] is the curvenumber; it represnts a single trace
hoverer = "function(el, x) {
            el.on('plotly_hover', function(d){
              dt = {'fill': 'none'};
              Plotly.restyle(el.id, dt, [12]);
            });
            el.on('plotly_unhover', function(d){
              ft = {'fill':'tonexty'};
              Plotly.restyle(el.id, ft, [12]); 
            });
          }"

To visualize your plot with this event:要使用此事件可视化您的情节:

fig %>% htmlwidgets::onRender(hoverer)

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM