【问题标题】:Interactively change axis scale (linear/log) in Plotly image using R使用 R 在 Plotly 图像中交互式更改轴比例(线性/对数)
【发布时间】:2020-06-18 10:24:04
【问题描述】:

目标:创建交互式下拉菜单/按钮以更新 R 中 Plotly 图形的坐标轴比例。

问题:有很多关于使用layoutupdatemenus创建buttonslog plots的文档;但是,很难找到一个描述如何专门添加一个按钮来更改轴的比例。 stackoverflow 上的一些帖子在 python 中提供了执行此操作的解决方案,但我努力为 R 找到一个等效的解决方案。我在此处提供了一个基于 python 解决方案的解决方案/示例。

起点: 使用一个小样本数据集,我想创建一个可以将比例从线性变为对数并在图上有不同轨迹的图形。我已经提供了自己的解决方案,但如果其他人有更有创意的解决方案,请随时添加!

data <- data.frame(x = c(1, 2, 3), 
                   y = c(1000, 10000, 100000),
                   y2 = c(5000, 10000, 90000))

【问题讨论】:

    标签: r plotly scale r-plotly


    【解决方案1】:

    这是一个基于提供的link中python中的解决方案;知道嵌套所有lists 的深度有点棘手。如果您没有为跟踪添加可见性,则可以将其替换为引用数据集。

    library(plotly)
    library(magrittr)
    
    # Fake data
    data <- data.frame(x = c(1, 2, 3), 
                       y = c(1000, 10000, 100000),
                       y2 = c(5000, 10000, 90000))
    
    # Initial plot with two traces, one off
    fig <- plot_ly(data) %>% 
      add_trace(x = ~x, y = ~y, type = 'scatter', mode = 'lines', name = 'trace1') %>%
      add_trace(x = ~x, y = ~y2, type = 'scatter', mode = 'lines', name = 'trace2', visible = F)
    
    # Update plot using updatemenus, keep linear as first active, with first trace; second trace for log
    fig <- fig %>% layout(title = 'myplot',
                          updatemenus = list(list(
                            active = 0,
                            buttons= list(
                              list(label = 'linear',
                                   method = 'update',
                                   args = list(list(visible = c(T,F)), list(yaxis = list(type = 'linear')))),
                              list(label = 'log',
                                   method = 'update', 
                                   args = list(list(visible = c(F,T)), list(yaxis = list(type = 'log'))))))))
    
    

    输出如下所示:

    【讨论】:

      猜你喜欢
      • 2020-12-21
      • 1970-01-01
      • 2021-03-20
      • 2019-04-12
      • 2017-09-25
      • 2017-09-11
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      相关资源
      最近更新 更多