【问题标题】:plotly R bar chart update direct labels dataplotly R条形图更新直接标签数据
【发布时间】:2019-03-20 12:11:58
【问题描述】:

我有这段代码可以生成带有直接标签的条形图,我添加了一个按钮来切换到另一个数据,但是如何更新直接标签数据

library(plotly)


chart <- structure(list(Date3 = structure(c(17805, 17806, 17807, 17808
), class = "Date"), Installed_qty_piling = c(9000, 1, 741, 72), Installed_qty_piling_Delivery = c(2592, 
                                                                                                  250, 33, 24)), row.names = c(NA, -4L), class = c("tbl_df", 
                                                                                                                                                   "tbl", "data.frame"))




p <- plot_ly(chart, x = ~Date3, y = ~Installed_qty_piling,text=~Installed_qty_piling,textposition = 'auto', type = "bar",  name = "A", visible = T) %>%
  layout(
    title = "qty installed",
    yaxis = list(title = "qty"),
    updatemenus = list(
      list(
        y = 100,
        buttons = list(
          list(method = "restyle",
               args = list("y", list(chart$Installed_qty_piling)),  # put it in a list
               label = "piling"),
          list(method = "restyle",
               args = list("y", list(chart$Installed_qty_piling_Delivery)),  # put it in a list
               label = "piling Delivery")))
    ))
p

【问题讨论】:

    标签: r plotly r-plotly


    【解决方案1】:

    是的,我在其他地方找到了答案,也需要更新文本

    library(plotly)
    
    
    chart <- structure(list(Date3 = structure(c(17805, 17806, 17807, 17808
    ), class = "Date"), Installed_qty_piling = c(9000, 1, 741, 72), 
    Installed_qty_piling_Delivery = c(2592, 
    
    250, 33, 24)), row.names = c(NA, -4L), class = c("tbl_df", 
    
    "tbl", "data.frame"))
    plot_ly(chart, x = ~Date3, y = 
    ~Installed_qty_piling,text=~Installed_qty_piling,textposition = 'auto', type = "bar",  
    name = "A", visible = T) %>%
    layout(
       title = "qty installed",
       yaxis = list(title = "qty"),
    updatemenus = list(
      list(
        y = 100,
        buttons = list(
          list(method = "restyle",
               args = list(list(y = list(chart$Installed_qty_piling), text = list(chart$Installed_qty_piling))),
               label = "piling"),
          list(method = "restyle",
               args = list(list(y = list(chart$Installed_qty_piling_Delivery), text = list(chart$Installed_qty_piling_Delivery))), 
               label = "piling Delivery")))
    )
    

    )

    【讨论】:

      猜你喜欢
      • 2020-08-23
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2015-01-22
      • 2021-06-04
      相关资源
      最近更新 更多