【问题标题】:How to make scatterplot points open a hyperlink using ggplotly - R如何使用ggplotly-R使散点图点打开超链接
【发布时间】:2019-01-11 20:18:31
【问题描述】:

我想让我的散点图点可点击,并在点击时打开每个点各自的超链接。我正在尝试使用 ggplotly 来做到这一点。 使用 plotly() 构建绘图时,有一种简单的方法可以做到这一点(参见下面的第一个示例)。但是,在 ggplotly 上运行它时,单击时会打开一个空白网页。

有人知道我如何使用下面的代码来完成这项工作吗?代码的 OnRender 部分是否有解决方案(不熟悉 javascript)? 使用 plotly 的第一个代码块有效。第二个代码块是我使用 ggplotly 进行的基本尝试,但它不起作用。

任何关于这方面的指导都会很棒!提前致谢

library(plotly)
library(htmlwidgets)
library(dplyr)

#1. Using plotly
mtcars$url <- paste0(
  "http://google.com/#q=", 
  rownames(mtcars))
p <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
  add_markers(customdata = ~url)
onRender(p, "
         function(el, x) {
         el.on('plotly_click', function(d) {
         var url = d.points[0].customdata;
         window.open(url);
         });
         }
         ")

#2. Using ggplotly`
p <- ggplot(data = mtcars, aes(x = wt, y = mpg))+
  geom_point()
pp <- ggplotly(p)
pp  <- add_markers(pp, customdata = ~url)
onRender(pp, "
         function(el, x) {
         el.on('plotly_click', function(d) {
         var url = d.points[0].customdata;
         url
         window.open(url);
         });
         }
         ")

【问题讨论】:

    标签: r shiny r-plotly htmlwidgets ggplotly


    【解决方案1】:

    看起来add_markers(pp, customdata = ~url) 没有效果。这样做是有效的:

    p <- ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point()
    pp <- ggplotly(p)
    pp$x$data[[1]]$customdata <- mtcars$url
    #pp  <- add_markers(pp, customdata = ~url)
    ppp <- onRender(pp, "
             function(el, x) {
             el.on('plotly_click', function(d) {
             var url = d.points[0].customdata;
             //url
             window.open(url);
             });
             }
             ")
    

    【讨论】:

    • 谢谢你。它绝对有效,非常感谢。但是,您知道当您更改点的形状或向绘图输入注释时单击是否会被禁用。最终我的情节呈现在闪亮的仪表板上,我的散点具有不同的几何形状,我的情节有矩形注释。代码看起来像这样
    • p &lt;- ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point() + annotate('rect', xmin = 2, xmax = 6, ymin = 2, ymax = 6, fill="firebrick", alpha = .2) pp &lt;- ggplotly(p) pp pp$x$data[[1]]$customdata &lt;- mtcars$url #pp &lt;- add_markers(pp, customdata = ~url) ppp &lt;- onRender(pp, " function(el, x) { el.on('plotly_click', function(d) { var url = d.points[0].customdata; //url window.open(url); }); } ") ppp
    • 小心,如果您使用颜色、组、构面等,这将不起作用...
    【解决方案2】:

    这是 Stéphane 解决方案的更通用版本,在使用组、颜色、构面等时不起作用。如果您直接在 customdata 美学中指定您的 url,一切都很好:

    mtcars$url <- paste0("http://google.com/#q=", rownames(mtcars))
    
    p <- ggplot(data = mtcars, aes(x = wt, y = mpg, color = as.character(carb), customdata = url)) + 
    geom_point() + 
    facet_wrap(~cyl)
    pp <- ggplotly(p)
    ppp <- htmlwidgets::onRender(pp, "
         function(el, x) {
         el.on('plotly_click', function(d) {
         var url = d.points[0].customdata;
         //url
         window.open(url);
         });
         }
         ")
    

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 2016-05-13
      • 1970-01-01
      • 2018-10-07
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多