0

I am trying to plot a line using geom_line from the ggplot package. This line should be interactive and is dependent on the selected state and macro variable over a selected time series. While the majority of it seems to run fine, I cannot seem to get geom_line() to plot anything.

I have tried grouping the data, as well as using the as.numeric() function for the 'year' variable, but nothing seems to work.

Server:

server <- function(input, output){


  output$plot1 <- renderPlot({

 ggplot(data = filter(c1, state == input$state1), 
        aes_string(x = as.numeric("year"), y = input$macroVar, group = 1)) +
   geom_line() +
   scale_x_continuous(limits = input$years) +
   labs(title = paste(col_alias(input$state1)),
        x = paste("Year"),
        y = paste(col_alias2(input$macroVar))) +
        theme_bw()})
  output$plot2 <- renderPlot({

ggplot(data = filter(c1, state == input$state2),  
       aes_string(x = as.numeric("year"), y = input$macroVar, group = 1)) +
  geom_line() +
  scale_x_continuous(limits = input$years) +
  labs(title = paste(col_alias(input$state2)),
       x = paste("Year"),
       y = paste(col_alias2(input$macroVar))) +
      theme_bw() })  
}   

shinyApp(ui = ui, server = server)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 4
    Hey, welcome to stack overflow! Straight up, this strikes me as a ggplot question, not an R Shiny question. We ask for minimal reproducible examples. This doesn't look minimal If the issue is that ggplot will not create a line, start by trying to debug that, outside of a Shiny instance. Once you've had a go at that it will be more encouraging for people on here to help you out. – LachlanO Oct 08 '18 at 04:27
  • Furthermore, since we do not have your data `c1`, even without `shiny` this is unreproducible. When we suggest it should be *reproducible*, you have a good start with the code but not the data. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Oct 08 '18 at 04:42

1 Answers1

0

You can't mix string and non-string calls in aes_string. You do this with as.numeric("year"), mixing as.numeric() function with a string column name. Check out this reproducible example:

# does not work
ggplot(mtcars, aes_string('mpg', as.factor('cyl'))) +
  geom_point()

# works
ggplot(mtcars, aes_string('mpg', 'as.factor(cyl)')) +
  geom_point()

So, you could change your code to 'as.numeric(year)' as a quick fix. I don't much like that either, aes_string gets ugly when you start putting commands in quotes. Instead, I'd suggest either (a) solving the problem upstream by converting your year column to numeric before plotting, or (b) Update your code to modern techniques using. See the Quasiquotation section at the ?aes help page, with examples at the bottom of the help page and more in the linked dplyr vignette.

If this doesn't solve your problem or you need additional help, please make a reproducible example and include it in your question.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294