简体   繁体   English

ggiraph:同一ggplot上的不同悬停效果

[英]ggiraph: different hover effects on the same ggplot

My goal is to create an interactive polygon which includes a label and hover effect. 我的目标是创建一个包含标签和悬停效果的交互式多边形。 Here is an example: 这是一个例子:

library(dplyr)
library(ggplot2)
library(ggiraph)

df <- data_frame(
  x = c(0, 0, 3, 5, 3, 6, 0),
  y = c(0, 4, 6, 5, 3, 0, 0),
  tooltip = replicate(7, "Hello")
)

text_df <- data_frame(
  x = 2,
  y = 3,
  label = "TEST",
  tooltip = "Hello"
)

gg <- ggplot() +
  geom_polygon_interactive(
    data = df,
    aes(
      x = x, 
      y = y,
      tooltip = tooltip,
      data_id = tooltip
    )
  ) +
  geom_text_interactive(
    data = text_df,
    aes(
      x = x, 
      y = y,
      label = label,
      tooltip = tooltip,
    ),
    color = "white",
    size = 5
  )

ggiraph(
  ggobj = gg,
  hover_css = "fill:red;",
)

When I hover over any place inside the polygon (except for the label), its color changes and the text is still visible. 当我将鼠标悬停在多边形内的任何位置(标签除外)上时,其颜色都会更改,并且文本仍然可见。 So, everything is fine. 所以,一切都很好。 I would like to have the exact same effect for the label as well. 我也希望标签具有完全相同的效果。 I want the polygon to change color and the text to remain the same on hover over the label. 我希望将多边形更改颜色并将文本悬停在标签上时保持不变。 The attempt below fills the label, making it illegible. 下面的尝试会填满标签,使其难以辨认。

# using the same data frame from the above-given example 

gg2 <- ggplot() +
  geom_polygon_interactive(
    data = df,
    aes(
      x = x, 
      y = y,
      tooltip = tooltip,
      data_id = tooltip
    )
  ) +
  geom_text_interactive(
    data = df,
    aes(
      x = 2, 
      y = 3,
      label = "TEST",
      tooltip = tooltip,
      data_id = tooltip
    ),
    color = "white",
    size = 5
  )

ggiraph(
  ggobj = gg2,
  hover_css = "fill:red;",
)

Is it possible to tweak the hover effects for each ggplot component? 是否可以调整每个ggplot组件的悬停效果?

As you noted, because the hover css is filling both the polygon and the text, it becomes illegible. 如您所述,由于悬停css同时填充了多边形和文本,因此变得难以辨认。 Here is hacked solution that uses css to change how the polygon and text layer are affected by the hover. 这是使用css更改悬停如何影响多边形和文本层的黑客解决方案。 I only know how to make this work if you save the ggiraph widget as html and edit from there. 我只知道如果将ggiraph小部件另存为html并从中进行编辑,则如何使它起作用。 But I didn't change your code in R. 但是我没有在R中更改您的代码。

Save the gg2 ggiraph widget 保存gg2 ggiraph小部件

This can be done using htmlwidgets::saveWidget() , the RStudio Viewer, or some other way. 可以使用htmlwidgets::saveWidget() ,RStudio Viewer或其他方法来完成。 It is easiest if the file isn't selfcontained so that a folder of scripts and stylesheets is generated alongside the html file. 如果文件不是自包含的,那么最简单的做法是在html文件的旁边生成脚本和样式表的文件夹。

#For example:
htmlwidgets::saveWidget(ggiraph(ggobj = gg2,
                                hover_css = "fill:red;"),
                        file = "widget.html",
                        selfcontained = FALSE)

If you save as html from the RStudio Viewer, the javascript files are base64 encoded in the html file and need to be decoded to be changed. 如果您从RStudio Viewer中另存为html,则javascript文件是在html文件中以base64编码的,需要进行解码才能更改。 In the html file there are several <script src="data-application/x-javascript ... "></script> tags with the encoding. 在html文件中,有几个带有编码的<script src="data-application/x-javascript ... "></script>标签。 I found that the second to last one will be equivalent to the ggiraphjs.min.js once it is decoded. 我发现倒数第二个等同于ggiraphjs.min.js,一旦被解码。

Edit the ggiraphjs.min.js file 编辑ggiraphjs.min.js文件

In the dependencies folder next to the html, find the file widget_files/ggiraphjs-0.1.0/ggiraphjs.min.js . 在html旁边的依赖项文件夹中,找到文件widget_files / ggiraphjs-0.1.0 / ggiraphjs.min.js This is a minified file so some javascript parser would be helpful but not necessary. 这是一个缩小的文件,因此一些javascript解析器会有所帮助,但不是必需的。 Search for the location where the code adds css styles to the page. 搜索代码将css样式添加到页面的位置。 Look for this in the code. 在代码中查找。 If the code is pretty-printed there are extra spaces around, but this should be consistent. 如果代码打印精美,周围会有多余的空格,但这应该是一致的。

... "\\n.hover_" ... ...“ \\ n.hover_” ...

Here, we can change the css that is injected to the html. 在这里,我们可以更改注入到html中的css。 How you change it will depend on the ggplot2 geoms used and their svg equivalent. 如何更改它取决于所使用的ggplot2几何图形及其等效的svg。 In this case, of wanting to change color of the polygon and not the text, we can add polygon before the class to specify that the .hover class will only affect svg polygon objects: "\\npolygon.hover_" . 在这种情况下,要更改多边形而不是文本的颜色,我们可以在该类之前添加polygon ,以指定.hover类将仅影响svg多边形对象: "\\npolygon.hover_" Change and save the file. 更改并保存文件。

Be sure that the javascript is correctly linked from the html file. 确保从html文件正确链接了javascript。 If you didn't use a self-contained file, this is already done. 如果您不使用独立文件,则此操作已经完成。 If you used a self-contained file and have decoded the base64 file, you'll need to replace the original script tag with a <script type = "text/javascript"> tag and put the new javascript before a closing </script> tag, or put it in a separate file and link to it. 如果您使用的是自包含文件并已对base64文件进行了解码,则需要用<script type = "text/javascript">标记替换原始脚本标记,并将新的javascript放在结束</script>标记,或将其放在单独的文件中并链接到它。

Load the html file 加载HTML文件

Now that the javascript file is changed, the html will be rendered and add a polygon.hover class at the top of the document instead of the .hover class from before. 现在更改了javascript文件,将呈现html并在文档顶部添加一个polygon.hover类,而不是之前的.hover类。 Any hover actions will only be applied to the type of tags added here. 任何悬停操作都将仅应用于此处添加的标签类型。

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

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