英文:
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 <- 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
答案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'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论