使用 R 的重新样式按钮将颜色映射到变量上

mapping color over variables using Restyle Buttons of plotly using R

我正在尝试使用 Restyle 按钮将颜色映射到两个变量上,在更改颜色时保持 y 轴和 x 轴动态。当我在主图中添加 color=~fruit 时,它给出了我正在寻找的结果,但是在更改变量时我失去了轴的动态。我基本上想改变与水果有关的线条的颜色。下面是数据和我用来玩的代码。感谢您的帮助或提示!

图书馆

library(dplyr); library(plotly);

数据

dfake <- tibble(days = seq(1,100, by=1),
                 bask = seq(1,500, by=5),
                fruit = c(rep("grape", 50), 
                          rep("apple", 50)));

绘制代码

plot <- dfake %>%
        plot_ly(x = ~days, y = ~bask, text = ~fruit, 
                type = 'scatter', 
                mode = 'lines', 
                hoverinfo = 'text',
                transforms = list(
                        list(type = 'filter',
                             target = ~fruit,
                             operation = '=',
                             value = unique(dfake$fruit)[1]))) %>%
        layout(updatemenus = list(
                list(type = 'dropdown',
                     active = 1,
                     buttons = list(
                             list(method = "restyle",
                                  args = list("transforms[0].value", 
                                              unique(dfake$fruit)[1]),
                                  label = unique(dfake$fruit)[1]),
                             list(method = "restyle",
                                  args = list("transforms[0].value", 
                                              unique(dfake$fruit)[2]),
                                  label = unique(dfake$fruit)[2])))));

plot;

我终于得到了我要找的东西, 实际上非常简单,我只需要让我的 grapeapple 不在列 fruit 内,而是作为不同的列并用 add_trace 对待它们。设置一个不可见。在每个 add_trace 中,我可以自由地使用颜色、宽度等。 在这个组织之后,使用 button 更容易。 我希望这个简单的编码可以帮助别人。如果没有对不起。

dfake <- tibble(days = seq(1,100, by=1),
                grape = seq(1,500, by=5),
                apple = seq(501,1000, by=5))
fig <- plot_ly(dfake, x = ~days) %>%
        add_trace(y = ~grape, name = 'Bask',
                  mode = 'lines+markers', type = "scatter",
                  marker = list(color = 'blue'),
                  line = list(color = 'blue', width = 4)) %>%
        add_trace(y = ~apple, name = 'New', visible = F,
                  mode = 'lines+markers', type = "scatter",
                  marker = list(color = 'red'),
                  line = list(color = 'red', width = 4)) %>%
        layout(
                title = "Corona global Cases and Depths",
                xaxis = list(domain = c(0.1)),
                yaxis = list(title = "yaxis"),
                updatemenus = list(
                        list(y = 0.9,
                             buttons = list(
                                     list(method = "restyle",
                                          args = list("visible", list(TRUE, FALSE)),
                                          label = "Grape"),
                                     list(method = "restyle",
                                          args = list("visible", list(FALSE, TRUE)),
                                          label = "Apple")))))
fig

是的,不确定数据是否采用最佳格式。所以,我用以下方式摆弄这个:

  1. 做一个, where each Y-axis variable goes into individual column (refer to the tidyverse philosopy).
  2. 将线条逐层添加到
  3. 使用 updatemenus 获取交互式按钮和所需的可见性。
#Converting into a dataframe, mutating new columns for each fruit and getting their name:
df_dfake <- as.data.frame(dfake)
df_dfake <- df_dfake %>% mutate(fruit1_bask = case_when(fruit == "grape" ~ bask),
                                fruit2_bask = case_when(fruit == "apple" ~ bask))
fruit1 <- unique(dfake$fruit)[1]; fruit2 <- unique(dfake$fruit)[2];

#Plotly, adding layer by layer:
fig <- df_dfake %>% plot_ly(type = 'scatter', 
                            mode = 'lines', 
                            hoverinfo = 'text');
fig1 <- fig %>% add_lines(x = ~days , y = ~fruit1_bask, text = ~fruit,
                  line=list(color="#33CFA5"));
fig2 <- fig1 %>% add_lines(x = ~days, y = ~fruit2_bask, text = ~fruit,
                         line=list(color="#F06A6A")); 
fig2;  

图2

现在,updatemenus组件,制作交互按钮

updatemenus <- list(
  list(
    active = -1,
    type= 'buttons',
    buttons = list(
      list(
        label = unique(dfake$fruit)[1],
        method = "update",
        args = list(list(visible = c(FALSE, TRUE)),# this defines visibility on click
                    list(title = "fruit1",
                         annotations = list(c(), df_dfake$fruit1_bask)))),
      list(
        label = unique(dfake$fruit)[2],
        method = "update",
        args = list(list(visible = c(T, F)),# this defines visibility on click
                    list(title = "fruit2",
                         annotations = list(c(), df_dfake$fruit2_bask))))
      )
  )
)

fig3 <- fig2 %>% layout(title = "Apples & Oranges", showlegend=FALSE,
                      xaxis=list(title="Days"),
                      yaxis=list(title="Basket"),
                      updatemenus=updatemenus); fig

这会产生以下带有交互式按钮的图表:

图3

Check Update Button to learn more:)