ggplotly和多个突出显示功能

时间:2018-09-26 12:42:32

标签: r ggplot2 plotly highlight ggplotly

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

  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

1 个答案:

答案 0 :(得分:0)

我很确定目前无法拥有2个单独的突出显示功能。

但是,如果某人除了点击突出显示之外还希望悬停突出显示,可以使用DB::table('animals')->max('order'); 方法中的尖峰。

layout