Shiny, renderPlot() : problem with geom_line() and group

ggplot2
shiny
rstudio

#1

Hey guys, i recently got into Shiny and after searching for quite a while I don’t know what else to try.

The code itself is very basic and gives a few graphs, it works perfectly, as an example :

unempview %>%
filter (active_population>5000, division != "Grand Duchy of Luxembourg") %>%
ggplot(aes(year , unemp_rate_diff, group = division, colour = division)) +
labs(title = "Unemployement rate difference between regions and national average",
   subtitle = "From the year 2001 to 2015", caption = "Taken from your own datasets",
   x = "Year", y = "Unemployement Difference") +
geom_line() 

But when i try to apply it to Shiny i can’t figure out how to use group or colour. With only one choice selected (other being “None” or the same), graph works fine but if I select 2 distinct regions it links all the points together as if group = 1 regardless of what i tried. Can’t find a way to get colours in either.

If you guys could point out the logical mistake i’m doing or help in any way, i’d be grateful. Here’s the code :

ui <- fluidPage(
titlePanel("My first Shiny App"),

sidebarLayout(

mainPanel(plotOutput("plot"),
          textOutput("selected_choice")),

sidebarPanel(selectInput("choice1", 
                         label = "Choose a division",
                         choices = c("None",unempview$division),
                         selected = "None"),
             
             selectInput("choice2", 
                         label = "Choose another division (Optional)",
                         choices = c("None",unempview$division),
                         selected = "None"),
             
             checkboxInput("difference",
                           "Display difference with national rate",
                           value = FALSE)
             )
)
)

server <- function(input, output) {

output$plot = renderPlot(unempview %>%
filter(division == input$choice1 | division == input$choice2) %>%
ggplot(aes(year, if (input$difference == TRUE) unemp_rate_diff 
                 else unemployment_rate_in_percent),
       group = division, colour = division) +
labs(title = "Unemployement rate comparaison between regions",
     subtitle = "From the year 2001 to 2015",
     caption = "Taken from your own datasets",
     x = "Year",
     y = if (input$difference == TRUE) "Unemployement Difference"
         else 'Unemployement Rate') +
geom_line()
)
}