【问题标题】:Open hyperlink on click on an ggplot/plotly chart单击 ggplot/plotly 图表打开超链接
【发布时间】:2018-09-17 13:32:56
【问题描述】:

这是针对Add onclick open hyperlink event to an html widget created in R 提供的答案的后续问题。考虑以下示例:

library(ggplot2)
library(plotly)
library(htmlwidgets)
library(htmltools)
myData <- data.frame(
  x=c(1,2,3), 
  y=c(3,2,1),
  label=c("Google", "Bing", "R"),
  category=c("search", "search", "other"),
  urls=c("http://google.de", "http://bing.com", "http://r-project.org")
)

f <- function(p) {
  ply <- ggplotly(p)
  javascript <- HTML(paste("
   var myPlot = document.getElementById('", ply$elementId, "');
   myPlot.on('plotly_click', function(data){
   var urls = ['", paste(myData$urls, collapse = "', '"), "'];
   window.open(urls[data.points[0].pointNumber],'_blank');
   });", sep=''))  
  prependContent(ply, onStaticRenderComplete(javascript))
}

这按预期工作 - 单击任何点都会打开相应的 url:

f(ggplot(myData, aes(x=x, y=y)) + geom_point(aes(text=label)))

这不能按预期工作 - 索引不再匹配:

f(ggplot(myData, aes(x=x, y=y)) + geom_point(aes(text=label, color=category)))

情节对象不同,似乎pointNumber 不再保存所有点的绝对索引,而是(颜色)分组中的索引。

关于如何调整示例使其适用于一般用例以及颜色/填充分组的任何想法?

【问题讨论】:

    标签: r ggplot2 plotly htmlwidgets


    【解决方案1】:
    • plotly_click 事件提供数据跟踪的名称。
    • data是点击事件信息
    • data.points[0].data.name 是跟踪/类别名称

    我们可以将其拆分为category(就像aes 所做的那样)并传递给我们的JavaScript 函数,而不是传递展平的数据框

    var urls = ", toJSON(split(myData, myData$category)), ";
                           
    

    这给了我们以下 JSON

    {"other": [{"x":3,"y":1,"label":"R","category":"other","urls":"http://r-project.org"}],
     "search":[{"x":1,"y":3,"label":"Google","category":"search","urls":"http://google.de"},
               {"x":2,"y":2,"label":"Bing","category":"search","urls":"http://bing.com"}]
    } 
    

    然后由

    检索 URL
    window.open(urls[data.points[0].data.name][data.points[0].pointNumber]['urls'],'_blank');
                           
    

    即从提供的 JSON 中:我们从第一个(也是唯一的)点上(data.points[0])获取跟踪的名称(data.name)及其pointNumber(即跟踪中的第 n 个点) .


    完整代码

    library(ggplot2)
    library(plotly)
    library(htmlwidgets)
    library(htmltools)
    library(jsonlite)
    
    myData <- data.frame(
      x=c(1,2,3), 
      y=c(3,2,1),
      label=c("Google", "Bing", "R"),
      category=c("search", "search", "other"),
      urls=c("http://google.de", "http://bing.com", "http://r-project.org")
    )
    
    f <- function(p) {
      ply <- ggplotly(p)
      
      javascript <- HTML(paste("
                               var myPlot = document.getElementsByClassName('js-plotly-plot')[0];
                               myPlot.on('plotly_click', function(data){
                               var urls = ", toJSON(split(myData, myData$category)), ";
                               window.open(urls[data.points[0].data.name][data.points[0].pointNumber]['urls'],'_blank');
                               });", sep=''))  
      prependContent(ply, onStaticRenderComplete(javascript))
    }
    
    f(ggplot(myData, aes(x=x, y=y)) + geom_point(aes(text=label, color=category)))
    

    【讨论】:

    • 像魅力一样工作 - 感谢您的帮助和解释!
    • 点击对我来说没有任何反应。你确定这个解决方案仍然有效吗?
    • @geotheory: Plotly 从 ggplotly 中删除了 elementID,请参阅 cran.r-project.org/web/packages/plotly/news/news.html 。该示例已更新,希望它也适用于您的最新版本。
    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多