简体   繁体   English

为 hover 上的每一层分别设置 css 属性(ggiraph)

[英]set css property separately for each layer on hover (ggiraph)

Using ggiraph , I'd like to set css properties differently for each ggplot geom_ or layer using hover.使用ggiraph ,我想使用 hover 为每个ggplot geom_或层设置不同的 ZC7A62 css属性。 In the example below, how might I set the stroke for the second geom_rect_interactive to blue on hover but keep the first layer stroke red on hover (keeping the data_id the same so both respond to hovering over either layer)?在下面的示例中,如何在 hover 上将第二个geom_rect_interactive的笔触设置为蓝色,但在 hover 上将第一层笔触保持为红色(保持data_id相同,以便两者都响应悬停在任一层上)?

library(ggplot2)
library(ggiraph)
p <- 
  ggplot(
  data = data.frame(id = seq(3), x = 1:3, y = 1:3)
) +

  # red stroke
  geom_rect_interactive(
    mapping = aes(
      xmin = x, xmax = x - 0.1,
      ymin = y, ymax = y - 0.1,
      data_id = id
    )
  ) +

  # blue stroke
  geom_rect_interactive(
    mapping = aes(
      xmin = x, xmax = x + 0.1,
      ymin = y, ymax = y + 0.1,
      data_id = id
    )
  )

x <- girafe(ggobj = p)
x <- girafe_options(
  x,
  opts_hover(
    css = "stroke:#ff0000;"
  )
)
x

I'm guessing I might be able to do something like assign some custom css class to a particular layer (eg, .mystroke {stroke:#0000ff;} ) but not sure how to approach this.我猜我可能能够做一些事情,比如将一些自定义css 8CBA22E28EB17B5F5C6AE2A266AZ class 分配给特定层(例如, .mystroke {stroke:#0000ff;} ),但不知道如何处理这个问题。

(I am one of the authors) This is not currently possible, we have not considered this case (we will if we can implement it). (我是作者之一)这目前是不可能的,我们还没有考虑过这种情况(如果我们可以实现它,我们会考虑)。

For the moment, it is only possible to specify a CSS per shape type.目前,只能为每种形状类型指定 CSS。 An example will be more meaningful, it is copied from:一个例子会更有意义,它复制自:

https://davidgohel.github.io/ggiraph/articles/offcran/customizing.html#detailled-control-1 https://davidgohel.github.io/ggiraph/articles/offcran/customizing.html#detailled-control-1

library(ggplot2)
library(ggiraph)


dataset <- mtcars
dataset$carname <- row.names(dataset)

gg_scatter <- ggplot(dataset, aes(x = disp, y = qsec, label = carname, 
  data_id = carname, color= wt) ) + 
  geom_point_interactive(size=3) + 
  geom_text_interactive(vjust = 2) +
  theme_minimal()

girafe(ggobj = gg_scatter2, 
  options = list(
    opts_hover(
    css = girafe_css(
      css = "fill:purple;stroke:black;",
      text = "stroke:none;fill:red;"
    )
  )
  ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM