简体   繁体   English

ggiraph:带超链接的工具提示?

[英]ggiraph: tooltip with hyperlink?

I am trying to create an interactive scatterplot with ggiraph where the tooltip allows me to navigate to a webaddress (which pertains to the specific selected dot).我正在尝试使用ggiraph创建一个交互式散点图,其中工具提示允许我导航到一个网址(与特定选定的点有关)。 Any idea whether this is actually possible and how to go about it?知道这是否真的可能以及如何去做吗? Many thanks for any advice!非常感谢您的任何建议!

library(tidyverse)
library(ggiraph)

my_df <- data.frame(stringsAsFactors=FALSE,
            x = c(0.5, 0.1),
            y = c(0.2, 0.9),
         link = c("bbcnews.com", "nyt.com"),
   link_name = c("bbc news", "nytimes")
)


my_plot <- my_df %>% 
  ggplot()+
  geom_point_interactive(aes(x=x,
                             y=y,
                             tooltip=paste0(link_name, 
                                            "\n",
                                            link)))
my_plot
girafe(ggobj=my_plot,
       height_svg = 5,
       width_svg = 5)

在此处输入图片说明

You can provide links through either the tooltip (written as html) or the onclick aesthetics.您可以通过tooltip (以 html 格式编写)或onclick美学提供链接。 Personally, I prefer to use the onclick since the tooltip almost always disappears when you move the mouse cursor to click on a link.就我个人而言,我更喜欢使用onclick因为当您移动鼠标光标单击链接时,工具提示几乎总是会消失。

In the code below I've tried to add both, so you can try to click on the point itself or see if you are fast enough to click the link in the tooltip.在下面的代码中,我尝试添加两者,因此您可以尝试单击该点本身或查看您是否足够快以单击工具提示中的链接。

library(tidyverse)
library(ggiraph)

my_df <- data.frame(stringsAsFactors=FALSE,
            x = c(0.5, 0.1),
            y = c(0.2, 0.9),
         link = c("http://bbcnews.com", "http://nyt.com"),
   link_name = c("bbc news", "nytimes")
)

my_plot <- my_df %>% 
  ggplot()+
  geom_point_interactive(aes(x=x,
                             y=y,
                             tooltip=paste0("<a href='", link, "'>",link_name, 
                                            "</a>\n",
                                            link), 
onclick=paste0('window.open("', link , '")')))

girafe(ggobj=my_plot,
       height_svg = 5,
       width_svg = 5)

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

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