R Shiny中的Ggplot2图形根据用户输入带有线

梅拉尼亚(Melania)CB

我想在我的Shiny App中制作一个图形,ggplot2用于按主题绘制有关用户选择的线。在这个问题中,我被告知如何在给定主题没有任何项目的情况下添加那些0(使用ankrun的答案)。现在,我正在寻找在闪亮的应用程序中拟合该图的想法,并考虑添加一个slideInput,您可以在其中选择该行的主题。这是我尝试过的:

# # # 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并且没有给出任何图形,只有一个空的灰色框。我不确定如何在图形代码中适合输入内容,我必须将其更改为错误!

戴夫·格鲁内瓦尔德(Dave Gruenewald)

这个问题是,您基本上是dtd2$Topic根据中的内容来重构selectInput,但是由于Topic仅有9个级别,并且您最多可以选择12个级别selectInput,因此会出现错误。

因此,我建议使用@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,而不要使用vanilla shiny::selectInput不过,我将由您决定。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用ggplot2在R中制作具有透明背景的图形?

使用R将带箭头的单箭头添加到ggplot2中的图形中

删除R中的ggplot2中的单个x轴刻度线?

R中带有分组列的ggplot2 barplot,如何拆栈?

R中带有ggplot2的rgb()

条形图由线连接/如何连接两个以grid.arrange排列的图形R / ggplot2

刻度线与ggplot2中的面板未对齐[R]

如何根据R Shiny中的用户输入来呈现不同数量的表?

在R Shiny中,根据用户输入更新对象

带有ggplot2(R)的每系列密度单条coutour线2d

r-markdown生成的PDF文件中是否可能有带有超链接的有线单元?

R ggplot2 geom_ribbon:当阴影线/着色区域在没有线位于下方且线没有位于上方时,由两侧的两条交叉线所界定

Shiny R中没有输出图形

R中带有ggplot2的堆叠Barplot

在R中绘制带有循环的图形

有线R响应

产生动态/多个输入框以根据Shiny R中的用户选择收集数据

带有ggplot2的R中的条形图

R语言:如何使用ggplot2在一张具有回归线的图形上绘制多个矢量?

Shiny + ggplot2:根据用户输入在一张图中绘制多条线

如何根据用户输入在 R Shiny 中呈现不同的绘图输出类型?

更改 ggplot2 中次要网格线的数量(每个主要网格线),r

更改 R 中 ggplot2 图形上的标签数量

在 R 中绑定多个 csv 以输出到 ggplot2 中的图形

r 中带有 geom_point 的 gg 绘图(ggplot2)

带有 ggplot2 的散点图,按 r 中的特定日期间隔着色

如何返回预期的图形(ggplot2,r 中的椭圆)?

在r中的ggplot2中的日期之间绘制水平线

在 R ggplot2 中向图形添加趋势线时出现问题