【问题标题】:Shiny: Subsetting ggplot based on input$var in .Rmd fileShiny:基于 .Rmd 文件中的 input$var 对 ggplot 进行子集化
【发布时间】:2023-03-10 22:27:01
【问题描述】:

我在为每个程序 (1:7) 显示我的 ggplots 时遇到问题。我在下面定义了一个输入侧边栏。但是,除了水平线之外,我最终得到了一个空白图,它不依赖于 DF。非常感谢任何帮助。

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

和:

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) +
})

使用可复制数据进行编辑 这是我的数据框的结构: 尝试绘制每个调查的每月累积平均值 #。

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

【问题讨论】:

  • 欢迎来到 SO!从代码 sn-ps 只能猜测可能是什么问题。为了帮助我们帮助您,您能否通过分享您的数据样本、您尝试过的代码和包 你用过吗?见how to make a minimal reproducible example
  • 您好 ttam10。仍然不是一个可重复的例子。两点说明:由于月份是您必须使用组 aes 的一个因素,即将group=1 添加到 aes()。以防万一:在您的代码中,geom_line 的末尾有一个 + ... 解决这个问题给了我一个情节。如果这不能解决您的问题,恐怕您必须编辑您的问题以使您的问题可重现。
  • 您好 Stefan,在帖子中添加了一个额外的 + 而不是初始代码,我仍然遇到 aes() 中指定的 group = 1 的问题。
  • 嗯。也许我错过了一些东西,但是......当我将您的数据和代码复制并粘贴到 Rmd 并运行文档时,一切正常,并给出了一个线图 + 两条线。
  • 我正在使用 Shiny Rmd 文档。我可以生成折线图,但是,我不能在带有输入侧边栏功能的 Shiny Rmd 文档中?下拉菜单。

标签: r ggplot2 shiny r-markdown


【解决方案1】:

也许这有助于解决您的问题。此外,这可以作为可重现示例的示例:


    ---
    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')
    }) 
    ```

【讨论】:

    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 2020-04-27
    相关资源
    最近更新 更多