R 绘图自定义数据格式变体
我正在尝试通过JavasCrit访问分配给每个数据点的customData
(例如,将超链接分配到每个点)。但是,我注意到数据格式从一个图变为另一个图,这似乎很奇怪。
这在此示例中非常有效(基于 a> post),也就是fill
美学是targue
vector:
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)
可以看出,单击每个点时会显示一个单一的自定义数据。 CustomData的结构类似于其他图。
但是,当将z
作为数值变量输入时,CustomData的结构很奇怪:
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)
这是一个错误吗?我该如何解决?实际上,我的数据集比本示例中的数据集大得多。
编辑
@ismirsehregal提供了适用于上述示例的解决方案。但是,看来customdata
按照的方式将向量数据采用,这似乎是完全错误的。因此,如果我们更改x
的顺序,js
脚本停止工作。
展示柜:
library(plotly)
library(htmlwidgets)
js <- "function(el, x) {
el.on('plotly_click', function(d) {
var zdimensions = [ 0, 0 ];
var vecindex = 0;
var clickedcustomdata = '';
var zdimensions = [ d.points[0].data.z.length, d.points[0].data.z[0].length ];
vecindex = d.points[0].pointIndex[0]*zdimensions[1] + d.points[0].pointIndex[1];
clickedcustomdata = d.points[0].data.customdata[vecindex];
console.log(d);
console.log(clickedcustomdata);
});
}"
X <- data.frame(x = c(1, 2, 3, 1, 2, 3),
y = c(1, 1, 1, 2, 2, 2),
z = 1:6,
mydata = letters[1:6])
X <- X[order(-X$z),]
gg <- ggplot(X, aes(x = x, y = y, customdata = mydata)) + geom_tile(aes(fill = z))
fig <- ggplotly(gg) %>% onRender(js)
fig
我想一个潜在的解决方案是先前订购X
,因此它与矩阵顺序匹配吗?
I am trying to access the customdata
assigned to each data point via the javascrit (for instance to assign an hyperlink to each point). However, I noticed that the data format changes from one plot to the other, which seems bizarre.
This works perfectly in this example (based on this post), that is when the fill
aesthetic is a character
vector:
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.
However, when z
is entered as a numerical variable, the structure of the customdata behaves bizarrely:
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.
Edit
@ismirsehregal provided a solution that works for the above example. However, it seems that customdata
takes the vector data as is, which seems quite wrong. Therefore, if we change the order of X
the js
script stop working.
Showcase:
library(plotly)
library(htmlwidgets)
js <- "function(el, x) {
el.on('plotly_click', function(d) {
var zdimensions = [ 0, 0 ];
var vecindex = 0;
var clickedcustomdata = '';
var zdimensions = [ d.points[0].data.z.length, d.points[0].data.z[0].length ];
vecindex = d.points[0].pointIndex[0]*zdimensions[1] + d.points[0].pointIndex[1];
clickedcustomdata = d.points[0].data.customdata[vecindex];
console.log(d);
console.log(clickedcustomdata);
});
}"
X <- data.frame(x = c(1, 2, 3, 1, 2, 3),
y = c(1, 1, 1, 2, 2, 2),
z = 1:6,
mydata = letters[1:6])
X <- X[order(-X$z),]
gg <- ggplot(X, aes(x = x, y = y, customdata = mydata)) + geom_tile(aes(fill = z))
fig <- ggplotly(gg) %>% onRender(js)
fig
I guess a potential solution would be to previously order X
in R so it matches the matrix order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是基于
PointIndex
的解决方法。我正在根据提供的
customdata
根据提供的pointIndex
:此对此很有用。
仍然我鼓励您在Github上提出问题,因此这是正式修复的。
This is a workaround based on the
pointIndex
.I'm calculating the vector index for
customdata
based on the providedpointIndex
:This was useful to get here.
Still I'd encourage you to file an issue on GitHub, so this is fixed officially.