Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

R Shiny + plotly legend in sidebar

I posted this on stack exchange, but I figured I would try here as well.

I’d like to put the legend to one of my graphs in the sidebar of my shiny fluid-page. I had two ideas for this: 1) Get the legend grob via ggplotly and put in sidebar or 2) render a legend with the same colors and no plot in the sidebar. Unfortunately I’m not sure how to do either of these. Any ideas? Example shiny below.

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)

Did you see these? … there are a few examples of different uses of plotly + shiny here: