首页 > 解决方案 > 使用 R 绘图的 Restyle Buttons 将颜色映射到变量上

问题描述

我正在尝试使用在更改颜色时保持 y 轴和 x 轴动态的Restyle按钮将当我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;

标签: rplotlydata-visualizationr-plotly

解决方案


Yes, wasn't sure of the data was in the best possible format. So, I was fiddling with this in the following manner:

  1. Make a , where each Y-axis variable goes into individual column (refer to the tidyverse philosopy).
  2. Appending the lines layer-by-layer into .
  3. Using updatemenus to get the interactive buttons & desired visibility.
#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;  

fig2

enter image description here

Now, updatemenus component, to make the interactive buttons

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

Which results in the following graphs with interactive buttons:

fig3 enter image description here enter image description here enter image description here

Check Update Button to learn more :)


推荐阅读