R 绘图自定义数据格式变体

发布于 2025-01-19 08:03:58 字数 2967 浏览 4 评论 0原文

我正在尝试通过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)

enter image description here
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)

enter image description here
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

enter image description here

I guess a potential solution would be to previously order X in R so it matches the matrix order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2025-01-26 08:03:58

这是基于PointIndex的解决方法。

我正在根据提供的customdata根据提供的pointIndex

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(clickedcustomdata);
         });
       }"

X <- data.frame(x = c(1, 2, 1, 2, 1, 2), y = c(1, 1, 2, 2, 3, 3), z = 1:6, mydata = letters[1:6])
gg <- ggplot(X, aes(x = x, y = y, customdata = mydata)) + geom_tile(aes(fill = z))
fig <- ggplotly(gg) %>% onRender(js)
fig

“结果”

对此很有用。

仍然我鼓励您在Github上提出问题,因此这是正式修复的。

This is a workaround based on the pointIndex.

I'm calculating the vector index for customdata based on the provided pointIndex:

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(clickedcustomdata);
         });
       }"

X <- data.frame(x = c(1, 2, 1, 2, 1, 2), y = c(1, 1, 2, 2, 3, 3), z = 1:6, mydata = letters[1:6])
gg <- ggplot(X, aes(x = x, y = y, customdata = mydata)) + geom_tile(aes(fill = z))
fig <- ggplotly(gg) %>% onRender(js)
fig

result

This was useful to get here.

Still I'd encourage you to file an issue on GitHub, so this is fixed officially.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文