title: Shiny creating a reactive legend
body: <p>I am following a solution given in <a href="https://stackoverflow.com/questions/53163119/r-shiny-aesthetics-must-be-either-length-1-or-the-same-as-the-data-8-y">R shiny Aesthetics must be either length 1 or the same as the data (8): y</a> for that annoying problem which I have happily fixed.</p>
  
  <p>The next issue I want to solve is that I want my plot to have a reactive legend - I only want the legend to display what's actually chosen and on the plot</p>
  
  <p>I also want to set the colours of the lines to the ones I want. And finally I want to make sure the legend is always in the order that I specify</p>
  
  <p>Here is a reproducible example (the commented out code is my attempt at solving my own issues.</p>
  
  <p>As you can see, the commented out section is how I've tried to get the legend and colours I want:</p>
  
  <pre><code>library(shiny)
  library(tidyverse)
  library(reshape2)
  library(scales)
  
  time &lt;- seq(-9, 60, 1)
  var1 &lt;- rnorm(70, 35, 2)
  var2 &lt;- rnorm(70, 50, 2)
  var3 &lt;- rnorm(70, 24, 2)
  var4 &lt;- rnorm(70, 17, 2)
  
  data &lt;- data.frame(time = time, 
                     var1 = var1, 
                     var2 = var2,
                     var3 = var3,
                     var4 = var4)
  
  datamelt &lt;- melt(data, "time")
  
  p &lt;- ggplot(datamelt, aes(x = time, y = value, color = variable)) +
    # scale_color_manual(values = c(
    # 'first' = 'red',
    # 'second' = 'blue',
    #   'third' = 'green',
    #   'fourth' = 'orange'
    #   ),
    # breaks = c("first", "second", "third", "fourth")) +
    #  labs(color = 'Legend') +
    theme_classic() +
    theme(axis.ticks = element_blank()) +
    labs(title = 'it means nothing',
         subtitle = 'these are made up data') +
    theme(plot.title = element_text(hjust = 0.5),
          plot.subtitle = element_text(hjust = 0.5)) +
    scale_x_continuous(name ="a y variable", breaks = seq(-9, 60, 1)) +
    scale_y_continuous(name = "yep an x variable", 
                       breaks = seq(0, 60, 5), labels = comma) + geom_blank()
  
  
  ui &lt;- fluidPage(
    titlePanel("trying to make this work"),
    sidebarLayout(
      sidebarPanel(
        checkboxGroupInput("whichone", "Choose something:", 
                           choiceNames = c("first", 
                                           "second", 
                                           "third",
                                           "fourth"), 
                           choiceValues = c("var1", 
                                            "var2",
                                            "var3",
                                            "var4"))
      ),
      ###the plot
      mainPanel(
        plotOutput("plot")
      )
    )
  )
  
  server &lt;- function(input, output) {
     output$plot &lt;- renderPlot({
        data_filtered &lt;- datamelt %&gt;% filter(variable %in% input$whichone)
        p + geom_line(data = data_filtered)
     })
  }
  
  shinyApp(ui, server)
  </code></pre>