目标:创建交互式下拉菜单/按钮以更新R中的Plotly图形的轴比例。
问题:有关使用和创建按钮和对数图的大量文档。但是,很难找到一个描述如何专门添加按钮以更改轴的比例的按钮。一些关于stackoverflow的帖子提供了在python中执行此操作的解决方案,但我一直在努力寻找与R等效的解决方案。在此,我根据python解决方案提供了一个解决方案/示例。layout
updatemenus
起点:我希望使用少量示例数据集来创建一个图形,该图形的比例可以从线性变化为对数,并且在图上具有不同的迹线。我提供了自己的解决方案,但是如果其他人有更富创意的解决方案,请随时添加!
data <- data.frame(x = c(1, 2, 3),
y = c(1000, 10000, 100000),
y2 = c(5000, 10000, 90000))
这是基于提供的链接中python中的解决方案;要知道将所有list
s嵌套的深度有多难。如果您不向迹线添加可见性,则可以参考数据集将其替换。
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'))))))))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句