R侧边栏中的Shiny +情节图例?

Lau*_*lin 5 r shiny plotly

我想将图例放在闪亮的流体页面侧栏中的其中一张图表中。为此,我有两个想法:1)通过ggplotly获取图例grob并放入侧边栏,或2)用相同的颜色渲染图例,并在侧边栏中无图。不幸的是,我不确定如何执行这些操作。有任何想法吗?示例下面有光泽。

library(plotly, shiny)

data <- ChickWeight
ag_data <- aggregate(data$weight,
                     list(time = data$Time, diet = data$Diet),
                     mean)  

ui <- fluidPage(

  sidebarPanel("Put Legend Here"),
  mainPanel(
    plotlyOutput("chick_weight")
  )

) # End fluid page

server <- function(input, output) {

  output$chick_weight <- renderPlotly({
    plot_ly(
      x = c(ag_data$time),
      y = c(ag_data$x),
      type = "scatter",
      mode = "lines",
      split = c(ag_data$diet),
      showlegend = FALSE
    )
  })

} # End server function

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明