【问题标题】:Ggplot2 graph in R Shiny with line according to user inputR Shiny 中的 Ggplot2 图形,根据用户输入带有线条
【发布时间】:2020-04-27 06:59:34
【问题描述】:

我想在我的 Shiny 应用程序中使用ggplot2 制作一个图表,绘制关于用户按主题选择的线条。 In this question 有人告诉我如何添加那些 0,以防给定主题没有任何项目(使用 ankrun 的答案)。现在我正在寻找在一个闪亮的应用程序中拟合这个图表并考虑添加一个滑块输入,您可以在其中选择该行的主题。这是我尝试过的:

# # # global # # #
#1. App
if("shiny" %in% rownames(installed.packages()) == FALSE){ install.packages("shiny") }
library(shiny)
#2. Easier data handling
if("dplyr" %in% rownames(installed.packages()) == FALSE){ install.packages("dplyr") }
library(dplyr)
#3. Graphs
if("ggplot2" %in% rownames(installed.packages()) == FALSE){ install.packages("ggplot2") }
library(ggplot2)
#4. Completion in graphs
if("tidyr" %in% rownames(installed.packages()) == FALSE){ install.packages("tidyr") }
library(tidyr)

# # # ui # # #
ui <- fluidPage( 
  sidebarPanel(
    selectInput("select_topic_timeline", "What topic?",
                choices = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), 
                selected =  c("1", "2", "3", "4","5", "6", "7", "8", "9", "10", "11", "12"),
                multiple = T)
  ),#sidebarPanel
  mainPanel( 
    plotOutput("per_topic_timeline")
  ) #mainPanel
) #fluidPage

# # # server # # #
server <- function(input, output, session) {

  # TIMELINE PER TOPIC PER YEAR
  output$per_topic_timeline <- renderPlot({
  dtd2 <- structure(list(Topic = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                             1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                             3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                             5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                             7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 
                                             10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
                                             11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Topic 1", 
                                                                                                                     "Topic 10", "Topic 11", "Topic 12", "Topic 2", "Topic 3", "Topic 4", 
                                                                                                                     "Topic 5", "Topic 6", "Topic 7", "Topic 8", "Topic 9"), class = "factor"), 
                         Year = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 2L, 
                                            3L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
                                            3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
                                            9L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
                                            8L, 9L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 6L, 7L, 8L, 
                                            9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 
                                            6L, 7L, 8L, 9L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("2011", 
                                                                                                    "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019"
                                            ), class = "factor"), Count = c(3L, 3L, 3L, 5L, 5L, 11L, 
                                                                            17L, 14L, 4L, 1L, 1L, 4L, 2L, 3L, 9L, 4L, 2L, 1L, 3L, 4L, 
                                                                            5L, 18L, 23L, 19L, 15L, 1L, 5L, 6L, 8L, 11L, 17L, 7L, 1L, 
                                                                            3L, 6L, 4L, 20L, 21L, 18L, 12L, 3L, 1L, 1L, 2L, 5L, 5L, 11L, 
                                                                            5L, 2L, 1L, 1L, 2L, 2L, 5L, 7L, 23L, 9L, 1L, 1L, 2L, 3L, 
                                                                            6L, 4L, 9L, 8L, 1L, 1L, 6L, 2L, 3L, 3L, 1L, 3L, 2L, 5L, 7L, 
                                                                            11L, 11L, 28L, 11L, 2L, 1L, 2L, 2L, 5L, 6L, 5L, 16L, 3L, 
                                                                            4L, 2L, 2L, 7L, 6L, 8L, 6L)), row.names = c(NA, -96L), class = "data.frame")

  dtd2 %>%
      expand(Topic = factor(Topic, levels = gtools::mixedsort(levels(c(input$select_topic_timeline)))) ,
             Year = unique(Year)) %>% 
      left_join(dtd2) %>% 
      mutate(Count = replace_na(Count, 0)) %>%
      ggplot(aes(x = Year, y = Count), colour = c(input$select_topic_timeline), group = Topic) + 
      geom_point() +
      geom_line() +
      labs(x = "Year", y = NULL, title = "Timeline")
  })
}

shinyApp(ui,server)

它没有打印任何错误,但它说Warning: Column "Topic" joining factors with different levels, coercing to character vector 并且它没有给出任何图表,只有一个空的灰色框。我不确定如何将输入拟合到图形代码中,我一定是改错了!

【问题讨论】:

    标签: r ggplot2 shiny dplyr tidyr


    【解决方案1】:

    这个问题是你基本上是根据selectInput中的内容重构dtd2$Topic,但是由于Topic只有9个级别,而selectInput最多可以选择12个级别,你是出现错误。

    出于这个原因,我建议改用@Ronak Shah 的答案和tidyr::complete。

    一旦你completed 数据,你应该然后通过主题dplyr::filter 得到我认为是期望的结果:

    # # # global # # #
    #1. App
    if("shiny" %in% rownames(installed.packages()) == FALSE){ install.packages("shiny") }
    library(shiny)
    #2. Easier data handling
    if("dplyr" %in% rownames(installed.packages()) == FALSE){ install.packages("dplyr") }
    library(dplyr)
    #3. Graphs
    if("ggplot2" %in% rownames(installed.packages()) == FALSE){ install.packages("ggplot2") }
    library(ggplot2)
    #4. Completion in graphs
    if("tidyr" %in% rownames(installed.packages()) == FALSE){ install.packages("tidyr") }
    library(tidyr)
    
    # # # ui # # #
    ui <- fluidPage( 
      sidebarPanel(
        selectInput("select_topic_timeline", 
                    label = "What topic?",
                    choices = as.character(1:12), 
                    selected = as.character(1:12),
                    multiple = TRUE),
      ),#sidebarPanel
      mainPanel( 
        plotOutput("per_topic_timeline")
      ) #mainPanel
    ) #fluidPage
    
    # # # server # # #
    server <- function(input, output, session) {
    
      # TIMELINE PER TOPIC PER YEAR
      output$per_topic_timeline <- renderPlot({
        dtd2 <- structure(list(Topic = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                   1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                                   3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                                   5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                                   7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 
                                                   10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
                                                   11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Topic 1", 
                                                                                                                           "Topic 10", "Topic 11", "Topic 12", "Topic 2", "Topic 3", "Topic 4", 
                                                                                                                           "Topic 5", "Topic 6", "Topic 7", "Topic 8", "Topic 9"), class = "factor"), 
                               Year = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 2L, 
                                                  3L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
                                                  3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
                                                  9L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
                                                  8L, 9L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 6L, 7L, 8L, 
                                                  9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 
                                                  6L, 7L, 8L, 9L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("2011", 
                                                                                                          "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019"
                                                  ), class = "factor"), Count = c(3L, 3L, 3L, 5L, 5L, 11L, 
                                                                                  17L, 14L, 4L, 1L, 1L, 4L, 2L, 3L, 9L, 4L, 2L, 1L, 3L, 4L, 
                                                                                  5L, 18L, 23L, 19L, 15L, 1L, 5L, 6L, 8L, 11L, 17L, 7L, 1L, 
                                                                                  3L, 6L, 4L, 20L, 21L, 18L, 12L, 3L, 1L, 1L, 2L, 5L, 5L, 11L, 
                                                                                  5L, 2L, 1L, 1L, 2L, 2L, 5L, 7L, 23L, 9L, 1L, 1L, 2L, 3L, 
                                                                                  6L, 4L, 9L, 8L, 1L, 1L, 6L, 2L, 3L, 3L, 1L, 3L, 2L, 5L, 7L, 
                                                                                  11L, 11L, 28L, 11L, 2L, 1L, 2L, 2L, 5L, 6L, 5L, 16L, 3L, 
                                                                                  4L, 2L, 2L, 7L, 6L, 8L, 6L)), row.names = c(NA, -96L), class = "data.frame")
    
        dtd2 %>%
          complete(Topic, Year = unique(Year), fill = list(Count = 0)) %>% 
          filter(Topic %in% paste("Topic", input$select_topic_timeline)) %>% 
          ggplot(aes(x = Year, y = Count, colour = Topic, group = Topic)) + 
          geom_point() +
          geom_line() +
          labs(x = "Year", y = NULL, title = "Timeline")
      })
    }
    
    shinyApp(ui,server)
    

    作为旁注,我还建议对这个特定的输入使用 shinyWidgets 包,特别是 shinyWidgets::pickerInput,而不是原版的 shiny::selectInput。不过,我会把这个决定留给你。

    【讨论】:

    • 谢谢!太棒了,我实际上是在想一个更好的方法来设置这个输入但是shinyWidgets 说Package LibPath Version Priority Depends Imports LinkingTo Suggests Enhances License License_is_FOSS License_restricts_use OS_type Archs MD5sum NeedsCompilation Built,我该如何解决?
    • 嗯,这是您从install.packages("shinyWidgets") 电话中收到的错误信息?
    • 是的,这就是我尝试安装时得到的结果!
    • 我以前没见过这个,但看起来问题出在包许可证上。也许尝试从 CRAN 或 github 安装作为备份(使用 devtools::install_git())
    • 我不明白,使用了 devtools (...) 并打印了 Error: Failed to install "unknown package" from Git: Error in "git2r_remote_ls": unsupported URL protocol 但我使用了一个新的 R 控制台,它很容易安装!非常感谢您的帮助!
    猜你喜欢
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多