Plotly 下拉菜单未正确重新设计 'y'
Plotly drop down menu not restyling 'y' correctly
示例数据的代码如下:
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
)
这是情节的代码:
plot_ly(df, x = ~date, y = ~y, color = ~cut,
type = "scatter", mode = "lines") %>%
layout(
title = "Drop down menus - Change y",
xaxis = list(title = "x"),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
buttons = list(
list(method = "restyle",
args = list("y", list(~y)),
label = "Show Y"),
list(method = "restyle",
args = list("y", list(~z)),
label = "Show Z")))
))
发生的事情是我执行了代码,绘制了绘图,它看起来像我想要的那样:随着时间的推移分类变量的事物计数。目标是让下拉菜单将一个 y
变量换成另一个,在本例中为 z
.
根据文档中的一些玩具示例和此处的几个答案,我认为这应该可行。相反,它看起来像是所有的值都奇怪地折叠成一行。似乎没有办法在不重新运行代码的情况下 return 将绘图恢复到原始状态。
有人知道如何让它正常工作吗?
这是我的 plotly
版本:
packageVersion("plotly")
[1] ‘4.5.6.9000’
您可以通过
让它工作
- 首先创建一个 "empty" 情节:
plot_ly(df, x = ~date, color = ~cut)
add_trace
每个 y 值:add_trace(y = ~z, type = 'scatter', mode = 'lines', visible=F
) 但隐藏第二条轨迹
- 将
restyle
应用于hide/show每组:args = list("visible", append(rep(list(TRUE), 5), rep(list(FALSE), 5)))
Ubuntu 16 和 Windows 10 上 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)
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
)
这是情节的代码:
plot_ly(df, x = ~date, y = ~y, color = ~cut,
type = "scatter", mode = "lines") %>%
layout(
title = "Drop down menus - Change y",
xaxis = list(title = "x"),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
buttons = list(
list(method = "restyle",
args = list("y", list(~y)),
label = "Show Y"),
list(method = "restyle",
args = list("y", list(~z)),
label = "Show Z")))
))
发生的事情是我执行了代码,绘制了绘图,它看起来像我想要的那样:随着时间的推移分类变量的事物计数。目标是让下拉菜单将一个 y
变量换成另一个,在本例中为 z
.
根据文档中的一些玩具示例和此处的几个答案,我认为这应该可行。相反,它看起来像是所有的值都奇怪地折叠成一行。似乎没有办法在不重新运行代码的情况下 return 将绘图恢复到原始状态。
有人知道如何让它正常工作吗?
这是我的 plotly
版本:
packageVersion("plotly")
[1] ‘4.5.6.9000’
您可以通过
让它工作- 首先创建一个 "empty" 情节:
plot_ly(df, x = ~date, color = ~cut)
add_trace
每个 y 值:add_trace(y = ~z, type = 'scatter', mode = 'lines', visible=F
) 但隐藏第二条轨迹- 将
restyle
应用于hide/show每组:args = list("visible", append(rep(list(TRUE), 5), rep(list(FALSE), 5)))
Ubuntu 16 和 Windows 10 上 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")))
))