0

I am having issues displaying my ggplots for each respective program (1:7). I have an inputs sidebar defined below. However, I end up with a blank plot aside from horizontal lines, which don't depend on a DF. Any help is a greatly appreciated.

selectInput("survey", label = h3("Program"), choices = c("1" = 1, "2" = 2, "3" = 3, "4" = 4, "5" = 5, "6" = 6, "7" = 7), selected = 1)

and:

ggplot(cumulative_data[cumulative_data$survey == input$survey,], aes(x=month, y=cum_mean)) +
  geom_ribbon(aes(ymin=lower_ci, ymax=upper_ci)) +
  geom_point(aes(size= count), color="#440154FF") + 
  geom_line(aes(x = month, y = cum_mean), linetype=1, color="#440154FF", size = 1.25) +
})

Edit w/ reproduceable data Here is the structure of my data frame: Trying to plot the cumulative mean month-by-month for each survey #.

df <- as.data.frame(expand.grid(survey = 1:7, month = factor(month.name[1:12], levels = month.name)))
df$cum_mean <- round(runif(84, 8, 10), 1)
df <- df[order(df$survey),]
Inputs {.sidebar}
-----------------------------------------------------------------------
selectInput("survey", label = h3("Program"), choices = c("1" = 1, "2" = 2, "3" = 3, "4" = 4, "5" = 5, "6" = 6, "7" = 7), selected = 1)
Row
-----------------------------------------------------------------------

### By Program

renderPlot({
ggplot(df[df$survey == input$survey,], aes(x=month, y=cum_mean, group =1)) +
  geom_line(aes(x = month, y = cum_mean), linetype=1, color="#440154FF", size = 1.25) + 
  geom_hline(yintercept = 8.9, col = '#B8DE29FF', lty = 2, lwd = 1.25) +
  geom_hline(yintercept = 9.46, col = '#2D708EFF', lty = 2, lwd = 1.25, alpha = .7) + theme_minimal() +
  labs(x = 'Month', y = 'Score')
  })     

output with group =1

output with no group = 1

ttam10
  • 35
  • 5
  • 1
    Welcome to SO! From the code snippets one can only guess what could be the issue. To help us to help you could you please make your issue reproducible by sharing a sample of your **data**, a running example of the **code** you tried and the **packages** you used? See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – stefan Jan 05 '21 at 16:43
  • Hi ttam10. Still not a reproducible example. Two remarks: As month is a factor you have to make use of the group aes, i.e. add `group=1` to aes(). And just in case: In your code there is a `+` at the end of geom_line ... Fixing this gives me a plot. If this does not fix your issue I'm afraid you have to edit your question to make your issue reproducible. – stefan Jan 05 '21 at 19:17
  • Hello Stefan, added an extra ```+``` on the post not the initial code and I still run into issues with ```group = 1``` specified in aes(). – ttam10 Jan 05 '21 at 21:18
  • Hm. Maybe I miss something but ... when I copy & paste your data and code into an Rmd and run the document everything works fine and gives be a line plot + the two hlines. – stefan Jan 05 '21 at 23:02
  • I am using a Shiny Rmd document. I can produce the line graphs, however, I cannot in the Shiny Rmd document with the input sidebar function? dropdown menu. – ttam10 Jan 06 '21 at 14:06
  • Hi ttam10. I just added my reproducible example code. That's the reason why we insist on having a MRE. Without an MRE figuring out a solution is like poking around in the haystack. (; – stefan Jan 06 '21 at 14:18
  • Hello @stefan, My dummy example does work. However, my actual data does not. Would have characters as my select input change any bit of the sample code? I still receive a ```Aesthetics must be either length 1 or the same as the data (1): x and y``` error. Again, I appreciate your patience and assistance! – ttam10 Jan 06 '21 at 14:42
  • Not sure about that. I have had look at the example data you provided initially. As this dataset only contains a survey 1 I get the same error when selecting a survey > 1. The reason that in this case there is a conflict with the empty data frame and the `group=1`. Hence it sounds that somehow your filtering of the dataset returns an empty df for all or for some options of selected input. Therefore I would suggest to check this outside of your app, i.e. load your df in the workspace and do e.g. `df[df$survey == 1,]` and then for 2 and ... Also check what happens when you use a character. – stefan Jan 06 '21 at 15:16

1 Answers1

0

Maybe this helps to solve your issue. Additionally this may serve as an example of what is meant by a reproducible example:


    ---
    title: "Untitled"
    output: 
      flexdashboard::flex_dashboard:
        orientation: rows
    runtime: shiny
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    ```
    
    ```{r}
    library(ggplot2)
    ```
    
    Inputs {.sidebar}
    -----------------------------------------------------------------------
    
    ```{r echo=FALSE}
    selectInput("survey", label = h3("Program"), choices = c("1" = 1, "2" = 2, "3" = 3, "4" = 4, "5" = 5, "6" = 6, "7" = 7), selected = 1)
    ```
    
    Row
    -----------------------------------------------------------------------
    
    ### By Program
    
    ```{r, echo=FALSE}
    df <- as.data.frame(expand.grid(survey = 1:7, month = factor(month.name[1:12], levels = month.name)))
    df$cum_mean <- round(runif(84, 8, 10), 1)
    df <- df[order(df$survey),]
    
    renderPlot({
      ggplot(df[df$survey == input$survey,], aes(x=month, y=cum_mean, group =1)) +
        geom_line(aes(x = month, y = cum_mean), linetype=1, color="#440154FF", size = 1.25) + 
        geom_hline(yintercept = 8.9, col = '#B8DE29FF', lty = 2, lwd = 1.25) +
        geom_hline(yintercept = 9.46, col = '#2D708EFF', lty = 2, lwd = 1.25, alpha = .7) + theme_minimal() +
        labs(x = 'Month', y = 'Score')
    }) 
    ```

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51