我希望生成的图块geom_tile()
使用ggplotly
和打开超链接htmlwidgets
。散点图上的点已经有了答案。
这是我到目前为止所拥有的:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句