代码之家  ›  专栏  ›  技术社区  ›  SeGa

图形和多个突出显示功能

  •  1
  • SeGa  · 技术社区  · 6 年前

    我怎么用 highlight ggplotly 对象。

    1. 黑色的悬停酒吧
    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
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   SeGa    6 年前

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

    但是,如果有人只是想要一个悬停突出除了点击突出显示一个可以使用在 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"))