我试图通过javascrit访问分配给每个数据点的customdata
(例如, for each 点分配一个超链接).然而,我注意到数据格式从一个图变为另一个图,这似乎很奇怪.
在本例中(基于this篇文章),这非常有效,即fill
美学是character
向量:
library(plotly)
library(htmlwidgets)
js <- "function(el, x) {
el.on('plotly_click', function(d) {
console.log(d);
console.log(d.points[0].data.customdata[0]);
});
}"
X <- data.frame(x = c(1, 2, 1, 2), y = c(1, 1, 2, 2), z = as.character(1:4), mydata = letters[1:4])
gg <- ggplot(X, aes(x = x, y = y, customdata = mydata)) + geom_tile(aes(fill = z))
ggplotly(gg) %>% onRender(js)
As can be seen, a single and correct custom data shows up when clicking on each point. The structure of the customdata is similar to other plots.
然而,当输入z
作为数字变量时,customdata的 struct 表现得很奇怪:
X <- data.frame(x = c(1, 2, 1, 2), y = c(1, 1, 2, 2), z = 1:4, mydata = letters[1:4])
gg <- ggplot(X, aes(x = x, y = y, customdata = mydata)) + geom_tile(aes(fill = z))
ggplotly(gg) %>% onRender(js)
Is this a bug? How can I fix this? In fact, my dataset is much larger than the one in this example.