Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R Plotly-更改下拉列表将禁用boxplot的分组_R_Plotly - Fatal编程技术网

R Plotly-更改下拉列表将禁用boxplot的分组

R Plotly-更改下拉列表将禁用boxplot的分组,r,plotly,R,Plotly,我正在为y轴上绘制的变量创建一个带有下拉选项的箱线图。数据有4个“人”,每个观察值都有a或B类型。对于每个人1:4,图表为观察值a和B绘制条形图(示例:) 我可以创建它,但一旦我更改下拉列表,所有分组都会变得混乱。下面是示例代码: library(plotly) set.seed(123) x <- rep(1:4, 6) y1 <- rnorm(24, 5, 2) y2 <- rnorm(24, 2, 5) type <- rep(c("A", "A", "B", "

我正在为y轴上绘制的变量创建一个带有下拉选项的箱线图。数据有4个“人”,每个观察值都有a或B类型。对于每个人1:4,图表为观察值a和B绘制条形图(示例:)

我可以创建它,但一旦我更改下拉列表,所有分组都会变得混乱。下面是示例代码:

library(plotly)

set.seed(123)
x <- rep(1:4, 6)
y1 <- rnorm(24, 5, 2)
y2 <- rnorm(24, 2, 5)
type <- rep(c("A", "A", "B", "B", "B", "B", "A", "A"), 3)

df <- data.frame(x, y1, y2, type)


p <- plot_ly(df, x = ~x) %>%
  add_boxplot(y = ~y1, color = ~type, name = "First") %>%
  add_boxplot(y = ~y2, color = ~type, name = "Second", visible = F) %>%
  layout(
    boxmode = "group",
    title = "On/Off Box Plot",
    xaxis = list(domain = c(0.1, 1)),
    yaxis = list(title = "y"),
    updatemenus = list(

      list(
        y = 0.8,
        buttons = list(
          list(method = "restyle",
               args = list("visible", list(TRUE, FALSE)),
               label = "y1"),

          list(method = "restyle",
               args = list("visible", list(FALSE, TRUE)),
               label = "y2")))
    )
  )

p
library(plotly)
种子集(123)

x
updatemenus
的长度应该是4,因为实际上有4条记录道(一次可以看到2条)。请注意,对于组,每个图例项都有相应的跟踪。不幸的是,R的向量自动重复掩盖了这一点

即,
updatemenus
的值应为:

updatemenus = list(
  list(
    y = 0.8,
    buttons = list(
      list(method = "restyle",
           args = list("visible", list(TRUE, TRUE, FALSE, FALSE)),
           label = "y1"),

      list(method = "restyle",
           args = list("visible", list(FALSE, FALSE, TRUE, TRUE)),
           label = "y2")))
)