ggplotly and multiple highlight functions

时光总嘲笑我的痴心妄想 提交于 2021-02-07 10:31:15

问题


How can I use two or more highlight functions on a ggplotly object. In the following example I want to highlight

  1. the hovered bar in black
  2. the clicked bar in blue

When using 2 highlight functions, only the last one is used and it obviously overwrites the previous one. So how can I define different behaviour for click and hover-events?

Data:

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")}

Example:

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:


I am pretty sure its currently not possible to have 2 separate highlight functions.

But, if someone just wants a hover-highlighting in addition to a click-highlighting one can use a spike in the layout method.

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"))


来源:https://stackoverflow.com/questions/52518037/ggplotly-and-multiple-highlight-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!