Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

r - Shiny: Subsetting ggplot based on input$var in .Rmd file

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...