【问题标题】:R Plotly - add_trace that sums value by groupR Plotly - add_trace 按组对值求和
【发布时间】:2021-01-29 19:41:21
【问题描述】:

尝试构建一个按图分组的条形图,该条形图通过对列进行分组来添加一条线来对值求和。

这是一个例子:

if (interactive()) {
  library(plotly)
  
  year <- c("2020", "2020", "2021", "2021", "2022", "2022", "2023", "2023", "2024", "2024", "2025", "2025")
  foodType <- c("Vegetable", "Fruit", "Vegetable", "Fruit", "Vegetable", "Fruit", "Vegetable", "Fruit", "Vegetable", "Fruit", "Vegetable", "Fruit")
  foodQty <- c(10, 10, 11, 17, 21, 23, 3, 4, 16, 2, 7, 43)
  
  data <- data.frame(year, foodType, foodQty)

  
  ui <- fluidPage(
    plotlyOutput("foodPlot")
  )
  
  server <- function(input, output, session) {
    output$foodPlot <- renderPlotly({
      plot_ly(data, x = ~year, y = ~foodQty, type = 'bar', color = ~foodType,
              hoverinfo = 'text',
              hovertext = ~paste0(foodType, ' ', foodQty)) %>%
        add_trace(x = ~year, y = ~foodQty,
                  type = 'scatter', mode = 'lines', name = 'Total Qty',
                  line = list(color = 'red'),
                  hoverinfo = "text",
                  hovertext = ~paste(foodQty),
                  transforms = list(list(type = 'aggregate', groups = ~year,
                                         aggregations = list(list(target = 'y', func = 'sum', enabled = T))))) %>%
        layout(xaxis = list(title = "", tickangle = -90), yaxis = list(title = ""),
               barmode = 'group') %>%
        config(displaylogo = FALSE, collaborate = FALSE,
               modeBarButtonsToRemove =
                 list("sendDataToCloud","zoom2d","pan2d","select2d","lasso2d",
                      "zoomIn2d","zoomOut2d","autoScale2d","hoverClosestCartesian",
                      "hoverCompareCartesian"))
    })
  }
  shinyApp(ui, server)
}

我的目标是有一个红色的Total Qty行,总和为year。所以在 2020 年会是 20,在 2021 年会是 28,依此类推。在这个例子中,我尝试使用 transforms 属性,但它没有像我想象的那样工作。

我还尝试将add_trace 中的xy 值替换为

x = ~aggregate(data$foodQty, by=list(year=data$year), FUN=sum)$year,
y = ~aggregate(data$foodQty, by=list(year=data$year), FUN=sum)$x,

仍然没有运气。感谢您的帮助!

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    您可以使用inherit = FALSE 来避免将属性从plot_ly() 传递到add_trace,并通过aggregate 创建一个新数据集 - 请检查以下内容:

    library(shiny)
    library(plotly)
    
    year <- c("2020", "2020", "2021", "2021", "2022", "2022", "2023", "2023", "2024", "2024", "2025", "2025")
    foodType <- c("Vegetable", "Fruit", "Vegetable", "Fruit", "Vegetable", "Fruit", "Vegetable", "Fruit", "Vegetable", "Fruit", "Vegetable", "Fruit")
    foodQty <- c(10, 10, 11, 17, 21, 23, 3, 4, 16, 2, 7, 43)
    
    data <- data.frame(year, foodType, foodQty)
    
    ui <- fluidPage(
      plotlyOutput("foodPlot")
    )
    
    server <- function(input, output, session) {
      output$foodPlot <- renderPlotly({
        plot_ly(data, x = ~year, y = ~foodQty, type = 'bar', color = ~foodType,
                hoverinfo = 'text',
                hovertext = ~paste0(foodType, ' ', foodQty)) %>%
          add_trace(data = aggregate(foodQty ~ year, data = data, sum), x = ~year, y = ~foodQty,
                    type = 'scatter', mode = 'lines', name = 'Total Qty',
                    line = list(color = 'red'),
                    hoverinfo = "text",
                    hovertext = ~paste(foodQty), inherit = FALSE) %>%
          layout(xaxis = list(title = "", tickangle = -90), yaxis = list(title = ""),
                 barmode = 'group') %>%
          config(displaylogo = FALSE, collaborate = FALSE,
                 modeBarButtonsToRemove =
                   list("sendDataToCloud","zoom2d","pan2d","select2d","lasso2d",
                        "zoomIn2d","zoomOut2d","autoScale2d","hoverClosestCartesian",
                        "hoverCompareCartesian"))
      })
    }
    shinyApp(ui, server)
    

    【讨论】:

    • 啊,非常感谢。我不知道inherit 属性。
    猜你喜欢
    • 1970-01-01
    • 2017-12-08
    • 2021-12-02
    • 2021-10-09
    • 2023-03-04
    • 2021-08-02
    • 2017-03-06
    • 2021-04-25
    • 2018-04-25
    相关资源
    最近更新 更多