ggplotly 和多个突出显示功能

ggplotly and multiple highlight functions

如何在 ggplotly 对象上使用 两个或更多 highlight 函数。 在下面的示例中,我想突出显示

  1. 黑色悬停条
  2. 点击的蓝色栏

使用2个高亮功能时,只用到最后一个,明显覆盖了前一个。那么如何为点击和悬停事件定义不同的行为?

数据:

dnew <- {structure(list(time_stamp = structure(c(1514761200, 1514847600, 
                                                 1514934000, 1515020400, 1515106800, 1515193200, 1515279600, 1515366000, 
                                                 1515452400, 1515538800, 1515625200, 1515711600, 1515798000, 1515884400, 
                                                 1515970800, 1516057200, 1516143600, 1516230000, 1516316400, 1516402800 ), class = c("POSIXct", "POSIXt"), tzone = ""), q_all = c(9953L, 
                                                          12070L, 10327L, 8649L, 11244L, 14058L, 11548L, 8819L, 8430L, 
                                                          8733L, 8590L, 9330L, 10888L, 11271L, 9102L, 7833L, 6642L, 7752L, 
                                                          8098L, 9625L), quality_q_all = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 
                                                                                           8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), col = c("darkgreen", 
                                                                                                                                                        "darkgreen", "darkgreen", "darkgreen", "darkgreen", "darkgreen", 
                                                                                                                                                        "darkgreen", "darkgreen", "darkgreen", "darkgreen", "darkgreen", 
                                                                                                                                                        "darkgreen", "darkgreen", "darkgreen", "darkgreen", "darkgreen", 
                                                                                                                                                        "darkgreen", "darkgreen", "darkgreen", "darkgreen")), .Names = c("time_stamp", 
                                                                                                                                                                                                                         "q_all", "quality_q_all", "col"), row.names = c(NA, 20L), class = "data.frame")}

示例:

library(ggplot2)
library(plotly)
key <- highlight_key(dnew, ~time_stamp)
p <- suppressWarnings(ggplot() +
                        geom_col(data = key, aes(x = as.character(time_stamp), y = q_all),
                                 color="gray", fill = dnew$col, width = 1) +
                        theme(text = element_text(size=9), 
                              axis.text.x = element_text(angle=45, hjust=1)))


dp <- ggplotly(p, source = "src", dynamicTicks = T) %>%
  plotly::layout(dragmode = "zoom") %>%
  highlight(on = "plotly_hover", off = "plotly_doubleclick", color = "black") %>%
  highlight(on = "plotly_click", off = "plotly_doubleclick", color = "blue")
dp

我很确定目前不可能有 2 个单独的突出显示功能。

但是,如果有人除了点击突出显示之外还想要悬停突出显示,可以在 layout 方法中使用尖峰。

ggplotly(p, source = "src", dynamicTicks = T) %>%
  plotly::layout(dragmode = "zoom") %>%
  highlight(on = "plotly_click", off = "plotly_doubleclick", color = "blue") %>% 
  layout(xaxis=list(spikethickness=4, spikecolor="white"))