简体   繁体   English

R Plotly - 按组汇总值的 add_trace

[英]R Plotly - add_trace that sums value by group

Attempting to build a plotly grouped bar graph that adds a line summing the values by grouping a column.尝试构建一个按图分组的条形图,通过对列进行分组来添加一条线来汇总值。

Here's an example:下面是一个例子:

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)
}

My goal is to have one red Total Qty line that sums by year .我的目标是拥有一条year求和的红色Total Qty线。 So in 2020 it would be 20, in 2021 it would be 28, etc. In this example, I attempted to use the transforms property but it's not working as I thought it would.所以在 2020 年它将是 20,在 2021 年它将是 28,等等。在这个例子中,我尝试使用transforms属性,但它没有像我想象的那样工作。

I've also tried to replace the x and y values in the add_trace to我还尝试将add_tracexyadd_trace

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

Still no luck.仍然没有运气。 Thanks for your help!谢谢你的帮助!

You can use inherit = FALSE to avoid passing attributes from plot_ly() to add_trace and create a new dataset via aggregate - Please check the following:您可以使用inherit = FALSE来避免将属性从plot_ly()传递到add_trace并通过aggregate创建新数据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)

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM