plotly 串扰过滤器过滤错误,从变量中泄漏其他类别的值

Posted

技术标签:

【中文标题】plotly 串扰过滤器过滤错误,从变量中泄漏其他类别的值【英文标题】:plotly crosstalk filter is filtering wrong, leaking values from other categories from variable 【发布时间】:2021-11-01 01:32:06 【问题描述】:

当对分类变量使用 filter_select 时,串扰没有正确过滤,从其他类别中获取值。

可重现的例子:


df <- structure(list(weight = c(0.349, 0.336, 0.329, 0.331, 0.329, 
 0.329, 0.321, 0.317, 0.317, 0.349, 0.351, 0.353, 0.355, 0.355, 
 0.355, 0.355, 0.356, 0.356, 0.358, 0.356, 0.356), value = c("housewife", 
    "Merchant", "Unknown", "Technologist", 
   "Admin", "Student", "Social worker", "Unemployed", 
   "Consultant", "Home", "Food", 
  "Engineering", "Real Estate", "Tourism", "Repairment", "Transport", 
   "Navy", "Military", "Security", "Distribution", "Restaurant"
 ), variable = c("work", "work", "work", 
                 "work", "work", "work", "work", "work", 
                  "work", "sector", "sector", "sector", "sector", 
                  "sector", "sector", "sector", "sector", "sector", 
                   "sector", "sector", "sector")), class = "data.frame",
          row.names = c(NA,-21L))

>  df
   weight         value variable
1   0.349     housewife     work
2   0.336      Merchant     work
3   0.329       Unknown     work
4   0.331  Technologist     work
5   0.329         Admin     work
6   0.329       Student     work
7   0.321 Social worker     work
8   0.317    Unemployed     work
9   0.317    Consultant     work
10  0.349          Home   sector
11  0.351          Food   sector
12  0.353   Engineering   sector
13  0.355   Real Estate   sector
14  0.355       Tourism   sector
15  0.355    Repairment   sector
16  0.355     Transport   sector
17  0.356          Navy   sector
18  0.356      Military   sector
19  0.358      Security   sector
20  0.356  Distribution   sector
21  0.356    Restaurant   sector

df <- highlight_key(df)

library(plotly)
library(crosstalk)

widgets <- bscols(
  widths = c(12),
  filter_select("variable", "Choose Variable", df, ~variable)
)
bscols(
  widths = c(2,10), widgets, 
  plotly::plot_ly(df, y = ~ weight, x = ~ value) %>%
    add_lines() %>%
    layout(xaxis = list(nticks = 10,
                        tickformat = ".2f")
           , showlegend = F)  )

我得到以下错误输出:

【问题讨论】:

【参考方案1】:

您需要使用categoryarraycategoryorder 定义x 轴上的类别。在您的示例中,您可能会将xaxis 误认为yaxis

此外,add_lines 将按照value(x 值)的字母顺序连接weight(y 值)。因此add_lines 需要替换为add_pathstype = "scatter", mode = "lines"

代码

df <- highlight_key(df)

widgets <- bscols(
  widths = c(12),
  filter_select("variable", "Choose Variable", df, ~variable)
)

bscols(
  widths = c(2,10),
  widgets,
  plotly::plot_ly(df, y = ~ weight, x = ~ value,
                  type = "scatter", mode = "lines") %>%
    layout(xaxis = list(type = 'category',
                        categoryarray =  ~value,
                        categoryorder = "array"),
           yaxis = list(nticks = 10,
                        tickformat = ".2f"),
           showlegend = F))  

情节

【讨论】:

以上是关于plotly 串扰过滤器过滤错误,从变量中泄漏其他类别的值的主要内容,如果未能解决你的问题,请参考以下文章

在 Shiny 中通过串扰将 Plotly 与 DT 一起使用

闪亮的两个情节和串扰问题

如何纠正 R 函数中的变异和过滤错误

Plotly-Dash:- 文件上传后在 plotly dash 中进行多列过滤

通过R中的串扰使用选择框在R plotly图中选择默认值,使用静态html不闪亮

交互式多过滤 Dash Plotly GIS 地图——图不起作用