legendrank in R plotly – bug or user error?

huangapple go评论105阅读模式
英文:

legendrank in R plotly - bug or user error?

问题

我在使用 Plotly (4.10.1) 在 R 中遇到了不希望出现的行为,与图例出现的顺序有关。
我已在 https://github.com/plotly/plotly.R/issues/2233 上提出了一个正式的问题;然而,我承认可能是我误解或存在“用户错误”。如果有人能够在这里证明,我将整理一下并删除我的错误报告。

以下是示例重现。

library(tidyverse)
library(plotly)
(tot_df <- structure(list(
  rank = structure(c(
    1L, 1L, 1L, 1L, 2L, 2L, 2L,
    2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L
  ), levels = c(
    "1", "2", "3",
    "4"
  ), class = "factor"), value = c(
    1, 0.316227766016838, 2, 0.447213595499958,
    3, 0.547722557505166, 4, 0.632455532033676, 5, 0.707106781186548,
    3, 0.547722557505166, 2, 0.447213595499958, 1, 0.316227766016838
  ), name = c(
    "stat1", "stat_2", "stat1", "stat_2", "stat1", "stat_2",
    "stat1", "stat_2", "stat1", "stat_2", "stat1", "stat_2", "stat1",
    "stat_2", "stat1", "stat_2"
  ), `Display name` = structure(c(
    1L,
    1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L
  ), levels = c(
    "Reference",
    "Current"
  ), class = "factor"), series_name_text = structure(c(
    1L,
    2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L
  ), levels = c(
    "Reference stat1",
    "Reference stat_2", "Current stat1", "Current stat_2"
  ), class = "factor"),
  my_legend_rank = c(
    1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
    3L, 4L, 1L, 2L, 3L, 4L
  )
), row.names = c(NA, -16L), class = c(
  "tbl_df",
  "tbl", "data.frame"
))
)


px <- plot_ly(
  data = tot_df,
  width = 800,
  height = 600,
  x = ~rank,
  y = ~value,
  name = ~series_name_text,
  legendrank = ~my_legend_rank,
  color = ~`Display name`,
  colors = c("green", "blue"),
  textposition = "none",
  type = "bar",
  opacity = .5,
  yaxis = "y"
) |> layout(legend = list(
  orientation = "h",
  xanchor = "center",
  yanchor = "bottom",
  y = -0.3,
  x = 0.5
))

# table(tot_df$series_name_text,tot_df$my_legend_rank
#                  1 2 3 4
# Reference stat1  4 0 0 0
# Reference stat_2 0 4 0 0
# Current stat1    0 0 4 0
# Current stat_2   0 0 0 4

从视觉上看,我们可以看到这不被遵守。

英文:

I encountered unwanted behaviour using plotly (4.10.1) in R with regards to the order that legends appear.
I have raised a formal issue at https://github.com/plotly/plotly.R/issues/2233 ; however I accept that there may be misunderstanding or 'user error' on my part. If someone can demonstrate that here, I will clean house and remove my bug report.

Here is the reprex.

library(tidyverse)
library(plotly)
(tot_df &lt;- structure(list(
rank = structure(c(
1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L
), levels = c(
&quot;1&quot;, &quot;2&quot;, &quot;3&quot;,
&quot;4&quot;
), class = &quot;factor&quot;), value = c(
1, 0.316227766016838, 2, 0.447213595499958,
3, 0.547722557505166, 4, 0.632455532033676, 5, 0.707106781186548,
3, 0.547722557505166, 2, 0.447213595499958, 1, 0.316227766016838
), name = c(
&quot;stat1&quot;, &quot;stat_2&quot;, &quot;stat1&quot;, &quot;stat_2&quot;, &quot;stat1&quot;, &quot;stat_2&quot;,
&quot;stat1&quot;, &quot;stat_2&quot;, &quot;stat1&quot;, &quot;stat_2&quot;, &quot;stat1&quot;, &quot;stat_2&quot;, &quot;stat1&quot;,
&quot;stat_2&quot;, &quot;stat1&quot;, &quot;stat_2&quot;
), `Display name` = structure(c(
1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L
), levels = c(
&quot;Reference&quot;,
&quot;Current&quot;
), class = &quot;factor&quot;), series_name_text = structure(c(
1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L
), levels = c(
&quot;Reference stat1&quot;,
&quot;Reference stat_2&quot;, &quot;Current stat1&quot;, &quot;Current stat_2&quot;
), class = &quot;factor&quot;),
my_legend_rank = c(
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L
)
), row.names = c(NA, -16L), class = c(
&quot;tbl_df&quot;,
&quot;tbl&quot;, &quot;data.frame&quot;
))
)
px &lt;- plot_ly(
data = tot_df,
width = 800,
height = 600,
x = ~rank,
y = ~value,
name = ~series_name_text,
legendrank = ~my_legend_rank,
color = ~`Display name`,
colors = c(&quot;green&quot;, &quot;blue&quot;),
textposition = &quot;none&quot;,
type = &quot;bar&quot;,
opacity = .5,
yaxis = &quot;y&quot;
) |&gt; layout(legend = list(
orientation = &quot;h&quot;,
xanchor = &quot;center&quot;,
yanchor = &quot;bottom&quot;,
y = -0.3,
x = 0.5
))
# table(tot_df$series_name_text,tot_df$my_legend_rank
#                  1 2 3 4
# Reference stat1  4 0 0 0
# Reference stat_2 0 4 0 0
# Current stat1    0 0 4 0
# Current stat_2   0 0 0 4

visually we can see this is not honoured.
legendrank in R plotly – bug or user error?

答案1

得分: 1

I'm reproducing the additional info I provided to the issues tracker.
https://github.com/plotly/plotly.R/issues/2233#issuecomment-1422537826
I have done additionally analysis; and show the origin of the problem; and how I solve / hacked around the issue. but I believe this should be best addressed by plotly internals, or by improving the documentation?

pb_repair <- plotly_build(px)

pb_repair$x$data[[1]]$legendrank
#[1] 1 2 3 4
pb_repair$x$data[[1]]$name
#[1] Current stat1

4 Levels: Reference stat1 ... Current stat_2

This is surely not intended; eachname should have a single associated legendrank and not the full list of all legendranks?

forcing solution -

(to_do <- length(pb_repair$x$data))

for(i in seq_len(to_do)){
pb_repair$x$data[[i]]$legendrank <- as.integer(pb_repair$x$data[[i]]$name)
}

pb_repair

![image](https://user-images.githubusercontent.com/51788011/217533048-ce602e3d-23a0-49d7-90ae-21dfe4e64e70.png)
As can be seen in this image. now reference stat1 is indeed the first/leftmost
I consider this an ugly hack ; but it will have to do until the plotly developers respond...
<details>
<summary>英文:</summary>
I&#39;m reproducing the additional info I provided to the issues tracker .
https://github.com/plotly/plotly.R/issues/2233#issuecomment-1422537826
I have done additionally analysis; and show the origin of the problem; and how I solve / hacked around the issue. but I believe this should be best addressed by plotly internals, or by improving the documentation ? 

pb_repair <- plotly_build(px)

pb_repair$x$data[[1]]$legendrank
#[1] 1 2 3 4
pb_repair$x$data[[1]]$name
#[1] Current stat1

4 Levels: Reference stat1 ... Current stat_2

This is surely not intended; eachname should have a single associated legendrank and not the full list of all legendranks ?

forcing solution -

(to_do <- length(pb_repair$x$data))

for(i in seq_len(to_do)){
pb_repair$x$data[[i]]$legendrank <- as.integer(pb_repair$x$data[[i]]$name)
}

pb_repair

![image](https://user-images.githubusercontent.com/51788011/217533048-ce602e3d-23a0-49d7-90ae-21dfe4e64e70.png)
As can be seen in this image. now reference stat1 is indeed the first/leftmost
I consider this an ugly hack ; but it will have to do until the plotly developers respond...
</details>

huangapple
  • 本文由 发表于 2023年2月8日 17:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定