如何使用 ggplotly - R 使图块打开超链接

How to make tiles open a hyperlink using ggplotly - R

我希望从 geom_tile() 生成的图块使用 ggplotlyhtmlwidgets 打开超链接。散点图上的点已经有

这是我目前拥有的:

mtcars$url <- paste0("http://google.com/search?q=", gsub(" ", "+", rownames(mtcars)))

p <- ggplot(data = mtcars, aes(x = wt, y = mpg, fill = as.character(carb), customdata = url)) + 
  geom_tile(width = .2, height = 1)
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);
     });
     }
     ")

确实会打开一个新的 Web 浏览器选项卡,但它是空白的。

好像结构有点变了。

请检查以下内容:

library(plotly)

mtcars$url <- paste0("https://google.com/search?q=", gsub(" ", "+", rownames(mtcars)))

p <- ggplot(data = mtcars, aes(x = wt, y = mpg, fill = as.character(carb), customdata = url)) + 
  geom_tile(width = .2, height = 1)
pp <- ggplotly(p)

plotly_json(pp)

ppp <- htmlwidgets::onRender(pp, "
     function(el, x) {
     el.on('plotly_click', function(d) {
     // console.log(d);
     var url = d.points[0].data.customdata[0];
     window.open(url);
     });
     }
     ")
ppp