ggplotly和多个突出显示功能 [英] ggplotly and multiple highlight functions

查看:37
本文介绍了ggplotly和多个突出显示功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

使用2个突出显示功能时,仅使用最后一个功能,并且显然会覆盖前一个功能.那么如何为点击事件和悬停事件定义不同的行为?

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?

数据:

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个单独的突出显示功能.

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

但是,如果某人除了点击高亮显示之外还想要悬停突出显示,可以使用 layout 方法中的尖峰.

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

这篇关于ggplotly和多个突出显示功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆