一个滑块控制 R 中的多个子图

One slider controlling multiple subplots in R

我想用一个滑块来控制用plotly创建的多个子图。我在 Python 中找到了如下两个答案:

示例(第二个 link):

import plotly.graph_objs as go
from plotly.tools import make_subplots

fig = make_subplots(1, 2)

fig.add_scatter(y=[1, 3, 2], row=1, col=1, visible=True)
fig.add_scatter(y=[3, 1, 1.5], row=1, col=1, visible='legendonly')
fig.add_scatter(y=[2, 2, 1], row=1, col=1, visible='legendonly')
fig.add_scatter(y=[1, 3, 2], row=1, col=2, visible=True)
fig.add_scatter(y=[1.5, 2, 2.5], row=1, col=2, visible='legendonly')
fig.add_scatter(y=[2.5, 1.2, 2.9], row=1, col=2, visible='legendonly')

steps = []
for i in range(3):
    step = dict(
        method = 'restyle',  
        args = ['visible', ['legendonly'] * len(fig.data)],
    )
    step['args'][1][i] = True
    step['args'][1][i+3] = True
    steps.append(step)

sliders = [dict(
    steps = steps,
)]

fig.layout.sliders = sliders

go.FigureWidget(fig)

但我如何在 R 中实现这一点?

这实际上与 python 中的过程完全相同。这是从 this:

派生的示例
library(plotly)

df <- data.frame(x = 1:5, 
                 y = 1:5) 

# create steps for slider
steps <- list(
  list(args = list("marker.color", "red"), 
       label = "Red", 
       method = "restyle", 
       value = "1"
  ),
  list(args = list("marker.color", "green"), 
       label = "Green", 
       method = "restyle", 
       value = "2"
  ),
  list(args = list("marker.color", "blue"), 
       label = "Blue", 
       method = "restyle", 
       value = "3"
  )
)

p1 <- p2 <- df %>%
  plot_ly(x = ~x, y = ~y,
          mode = "markers", 
          marker = list(size = 20,
                        color = 'green'), 
          type = "scatter")

p <- subplot(p1, p2) %>%
  layout(title = "Basic Slider",
         sliders = list(
           list(
             active = 1, 
             currentvalue = list(prefix = "Color: "), 
             pad = list(t = 60), 
             steps = steps))) 

p