使用 R 以交互方式更改 Plotly 图像中的轴比例(线性/对数) [英] Interactively change axis scale (linear/log) in Plotly image using R

查看:46
本文介绍了使用 R 以交互方式更改 Plotly 图像中的轴比例(线性/对数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

问题:有很多关于创建

Goal: To create interactive dropdown/buttons to update the axes' scale for a Plotly figure from R.

Issue: There is a lot of documentation on creating buttons and log plots using layout and updatemenus; however, it was difficult to find one that described how a button could be added specifically for changing the scale of the axes. Some posts on stackoverflow provided solutions for doing this in python but I struggled to find an equivalent one for R. I have provided a solution/example here based upon the python solution.

Starting Point: With a small sample data-set, I want to create a graphic that can have the scale change from linear to log and have different traces on the plots. I have provided my own solution, but if anyone else has a more creative solution feel free to add!

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

解决方案

Here is a solution based upon the one in python from the provided link; it was a bit tricky to know how deep to nest all of the lists. If you are not adding visibility to the traces, you can replace it with reference to the dataset.

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'))))))))

The output looks like:

这篇关于使用 R 以交互方式更改 Plotly 图像中的轴比例(线性/对数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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