R 不重新设置样式的绘图下拉菜单';y';正确地

R 不重新设置样式的绘图下拉菜单';y';正确地,r,plotly,R,Plotly,下面是示例数据的代码: library(tidyverse) library(plotly) df <- data_frame( date = Sys.Date() - seq(0, 99, by = 0.2)[2:101], cut = rep(c("Ideal", "Premium", "Very Good", "Good", "Fair"), 20), y = sample_n(diamonds, size = 100)$depth, z = sample_n(di

下面是示例数据的代码:

library(tidyverse)
library(plotly)

df <- data_frame(
  date = Sys.Date() - seq(0, 99, by = 0.2)[2:101],
  cut = rep(c("Ideal", "Premium", "Very Good", "Good", "Fair"), 20),
  y = sample_n(diamonds, size = 100)$depth,
  z = sample_n(diamonds, size = 100)$price
)
所发生的事情是,我执行代码,绘制图,它看起来就像我希望的那样:随着时间的推移,分类变量的计数。目标是让下拉菜单将一个
y
变量替换为另一个变量,在本例中为
z

基于文档中的一些玩具示例和这里的几个答案,所以我觉得这应该是可行的。相反,看起来所有的值都奇怪地折叠成一行。似乎没有办法在不重新运行代码的情况下将绘图返回到其原始状态

有人知道如何让它正常工作吗

这是我的
版本:

packageVersion("plotly")
[1] ‘4.5.6.9000’

你可以让它工作的时间

  • 首先创建“空”绘图:
    plot\u ly(df,x=~date,color=~cut)
  • add_trace
    为每个y值:
    add_trace(y=~z,type='scatter',mode='lines',visible=F
    ),但隐藏第二个跟踪
  • 应用
    restyle
    隐藏/显示每个集合:
    args=list(“可见”、追加(rep(list(TRUE)、5)、rep(list(FALSE)、5))
在Ubuntu16和Windows10上的R-Studio上的plotly 4.5.6为我工作

library('tidyverse')
library('plotly')

df <- data_frame(
  date = Sys.Date() - seq(0, 99, by = 0.2)[2:101],
  cut = rep(c("Ideal", "Premium", "Very Good", "Good", "Fair"), 20),
  y = sample_n(diamonds, size = 100)$depth,
  z = sample_n(diamonds, size = 100)$price
)
p <- plot_ly(df, x = ~date, color = ~cut) %>%
add_trace(y = ~y, type = 'scatter', mode = 'lines', visible=T) %>%
add_trace(y = ~z, type = 'scatter', mode = 'lines', visible=F)

p <- p %>% layout(
  updatemenus = list(
    list(
      buttons = list(
        list(method = "restyle",
             args = list("visible", append(rep(list(TRUE), 5), rep(list(FALSE), 5))),
             label = "Show Y"),
        list(method = "restyle",
             args = list("visible", append(rep(list(FALSE), 5), rep(list(TRUE), 5))),
             label = "Show Z")))
))
library('tidyverse'))
库(“plotly”)
df%
添加_跟踪(y=~z,类型='scatter',模式='lines',可见=F)
p%布局(
updatemenus=列表(
名单(
按钮=列表(
列表(method=“restyle”,
args=list(“可见”、追加(rep(list(TRUE)、5)、rep(list(FALSE)、5)),
label=“Show Y”),
列表(method=“restyle”,
args=list(“可见”、追加(rep(list(FALSE)、5)、rep(list(TRUE)、5)),
label=“Show Z”)))
))

感谢@Maximilian Peters提供的帮助性解决方案!我也尝试过这样做,但我没有使用
add\u trace()
我使用了
add\u lines()
。不过,这可能无关紧要——看起来像是在
append(rep(list(T),5),rep(list(F),5))
中发生了奇迹——这是如何工作的?为什么
args
变量需要5次该值?是因为
cut
有五个唯一的分类值吗?再次感谢@罗伯特米切尔:我也被打败了。在
add_trace
中,给
visible
参数一次就足够了,但以后需要重复。可能是由于
cut
的缘故,跟踪随后被分割,并从
add\u trace
调用继承
visible
属性。
library('tidyverse')
library('plotly')

df <- data_frame(
  date = Sys.Date() - seq(0, 99, by = 0.2)[2:101],
  cut = rep(c("Ideal", "Premium", "Very Good", "Good", "Fair"), 20),
  y = sample_n(diamonds, size = 100)$depth,
  z = sample_n(diamonds, size = 100)$price
)
p <- plot_ly(df, x = ~date, color = ~cut) %>%
add_trace(y = ~y, type = 'scatter', mode = 'lines', visible=T) %>%
add_trace(y = ~z, type = 'scatter', mode = 'lines', visible=F)

p <- p %>% layout(
  updatemenus = list(
    list(
      buttons = list(
        list(method = "restyle",
             args = list("visible", append(rep(list(TRUE), 5), rep(list(FALSE), 5))),
             label = "Show Y"),
        list(method = "restyle",
             args = list("visible", append(rep(list(FALSE), 5), rep(list(TRUE), 5))),
             label = "Show Z")))
))