【问题标题】:R Plotly adding multiple line chart with dropdown menuR Plotly用下拉菜单添加多个折线图
【发布时间】:2017-03-07 19:40:39
【问题描述】:

我有一个如下所示的数据集:

    date       food    transp  housing  other
0   2015-06-01  1510    45.58   0       101.5
1   2015-07-01  1163.91 74.14   210     106.7
2   2015-08-01  101.3   95.03   210     54.5
3   2015-09-01  1131.67 22.28   210     46.3
4   2015-10-01  818.44  88.88   815.2   47.2

我的目标是链接chart中的图表“奥运奖牌”,它是通过python在plolty中实现的,我正在尝试在R中复制。我能够做的是this
乍一看,它看起来很相似,但我没有包含所有行的下拉菜单“全部”。目前没有办法恢复所有线条的情节,这与第一个图表中的选项“全部”不同。下面的代码显示了我如何创建我的第二个情节。非常感谢任何帮助。

plot <- plot_ly(newf, x = ~round_date, y = ~other, name='other', type='scatter',mode='lines+markers') %>%
add_trace(y = ~food, name = 'food') %>%
add_trace(y = ~housing, name = 'housing') %>%
add_trace(y = ~transport, name = 'transport') %>%
    layout(
title = "Button Restyle",
xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
updatemenus = list(
    list(
    type = "buttons",
  y = 0.8,
  label = 'Category',
  buttons = list(
    list(method = "restyle",
         args = list("y",list(~other)),
          label = "houshold"),

   list(method = "restyle",
         args = list("y",list(~food)),
          label = "communication"),

   list(method = "restyle",
         args = list("y",list(~housing)),
          label = "food"),

   list(method = "restyle",
         args = list("y",list(~transport)),
          label = "transport")

            )))) 

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    最好隐藏/显示轴,而不是更改 y 值。您可以通过将数组传递给可见属性来隐藏和显示轴,例如args = list('visible', c(TRUE, FALSE, FALSE)) 将显示第一个轴并隐藏另外两个。

    完整代码见下方或here在线。

    从散点图更改为条形图会在 y 值更改时搞砸重新设计的过程,您会得到 4 个颜色不同但 y 值相同的条形图。

    library(plotly)
    plot_ly(mtcars, x = rownames(mtcars), y = ~mpg, name='mpg', type='scatter', mode='markers') %>%
      add_trace(y = ~hp, name = 'power', type='scatter', mode='markers') %>%
      add_trace(y = ~qsec, name = 'qsec', type='scatter', mode='markers') %>%
      layout(
        updatemenus = list(
          list(
            type = "buttons",
            x = -0.1,
            y = 0.6,
            label = 'Category',
            buttons = list(
              list(method = "restyle",
                   args = list('visible', c(TRUE, TRUE, TRUE)),
                   label = "View all"),
              list(method = "restyle",
                 args = list('visible', c(FALSE, FALSE, FALSE)),
                 label = "Hide all")
            )
          ), 
          list(
            type = "buttons",
            x = -0.1,
            y = 0.7,
            label = 'Category',
            buttons = list(
              list(method = "restyle",
                   args = list('visible', c(TRUE, FALSE, FALSE)),
                   label = "mpg"),
              list(method = "restyle",
                   args = list('visible', c(FALSE, TRUE, FALSE)),
                   label = "hp"),
              list(method = "restyle",
                   args = list('visible', c(FALSE, FALSE, TRUE)),
                   label = "qsec")
            )
          )
        )
      )
    

    【讨论】:

    • 非常感谢,我只是不知道我可以隐藏和显示轴。