一个滑块控制 R 中的多个子图 [英] One slider controlling multiple subplots in R

查看:49
本文介绍了一个滑块控制 R 中的多个子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  • I want to use one slider to control multiple subplots created with plotly. I found answers in Python like these two:

    Example (second 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)
    

    But how can I realize this in R?

    解决方案

    It's actually quite the same procedure as in python. Here is an example derived from 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
    

    这篇关于一个滑块控制 R 中的多个子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆