繁体   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