簡體   English   中英

R Plotly - 按組匯總值的 add_trace

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

嘗試構建一個按圖分組的條形圖,通過對列進行分組來添加一條線來匯總值。

下面是一個例子:

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

我的目標是擁有一條year求和的紅色Total Qty線。 所以在 2020 年它將是 20,在 2021 年它將是 28,等等。在這個例子中,我嘗試使用transforms屬性,但它沒有像我想象的那樣工作。

我還嘗試將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,

仍然沒有運氣。 謝謝你的幫助!

您可以使用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