ggplot with 'scale_linetype_manual' not displaying correct linetype on Shiny Server

Hi all,

I've been working on an app with a colleague to visualize the COVID-19 data in the USA but am running into a strange issue. I have a ggplot where I want different lines to be a certain linetype depending on a column value. I use scale_linetype_manual to do this. It works perfectly well in RStudio during local testing, but once on the ShinyServer, the scale_linetype_manual seems to be ignored and the linetypes just take the default order of solid for the first line, second one dashed etc.

Here is a reprex app (the real one is much more complex)


ui <- fluidPage(
  checkboxGroupInput("line", "Pick a line", c("A", "B")),

server <- function(input, output, session) {
  data = data.frame(x = c(rep(1:10, 2)), y = c(runif(10), rep(2, 10)), 
                    group = c(rep("A", 10), rep("B", 10)))
  output$plot = renderPlot({
    ggplot(data %>% filter(group %in% input$line), 
           aes(x = x, y = y, linetype = group)) + 
      geom_line(size = 2) +
      scale_linetype_manual(values = c("dashed", "dotted"),
                            breaks = c("A", "B"),
                            labels = c("Group A", "Group B"))

shinyApp(ui, server)

As expected, in the app, if you select line A it will always be dashed, and B always dotted. However, in the app online, it will default to solid line if only one line is displayed and solid, dashed if two, regardless of which line is selected.

Does anyone know why this is happening? I've experimented and searched a long time but can't seem to find the problem...

The real app is hosted on and in the Testing tab, I would like the lines for Total = solid, Positive = dotted and Negative = dashed, regardless now many are selected. Now you'll see it defaults to solid if only one is selected.


1 Like

Would you mind including a couple of screenshots showing exactly what you expect and what you're actually seeing?


Thanks for the fast reply.

Here is a screenshot of the plot run locally:

And this is the exact same app after pushing it to GitHub (the server checks GitHub and loads all files form there) and then run on the server and seen online

As you can see, the online version does not respond to me forcing the Total line to be the solid one and negative to be dashed.

This happend when I only display the negative line


Line dashed as it should be


Line solid, although scale_linetype_manual should force it to be dashed

The code is exactly the same on both...


Nothing obvious comes to my mind, and given that the code is the same, unfortunately you're going to have to figure out exactly what is different. A few questions that I'd start by asking:

  • Do you have the same problem when you using your simpler reprex app?
  • Do you have exactly the same package versions installed in both places?
  • Does sort()ing the vector return the same results in both places? (i.e. is this something to do with the default encoding)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.