【问题标题】:How to switch between different ggplot types?如何在不同的ggplot类型之间切换?
【发布时间】:2020-12-22 08:45:43
【问题描述】:

我想要一个应用程序来绘制不同类型的数据(如线、点、箱线图)。

让我们考虑一下我的代码:

library(shiny)
library(quantmod)

start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
# Apple stock
getSymbols("AAPL", src = "yahoo", from = start, to = end)
apple <- AAPL$AAPL.Close

### Define UI for application
ui <- fluidPage(

  # Sidebar panel
  sidebarPanel(
    selectInput("type",
      label = "1. Select plot type",
      choices = c("Line" = "geom_line()", "Dot" = "geom_point()", "Boxplot" = "geom_boxplot()"),
      selected = 3
    )
  ),

  # Main Panel
  mainPanel(
    plotOutput("sp")
  )
)





server <- function(input, output) {
  output$sp <- renderPlot({
    colm <- as.numeric(apple)
    ggplot() +
      aes(x = 1:length(colm), y = colm) +
      geom_line() +
      xlab(NULL)
  })
}

# Run the application
shinyApp(ui = ui, server = server)

而且我不确定我在server 中输入了什么才能在这些不同类型之间切换。我尝试使用if 语句和get(input$type),但这两个选项都给我带来了错误。

总结 - 我如何编写我的应用程序,以便可以在不同的绘图类型(线、点、箱线图)之间切换,如图所示?

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    可以这样实现。你可以例如使用switch 语句根据类型分配geom_xxx

    library(shiny)
    library(quantmod)
    library(ggplot2)
    
    start <- as.Date("2013-01-01")
    end <- as.Date("2016-10-01")
    # Apple stock
    getSymbols("AAPL", src = "yahoo", from = start, to = end)
    apple <- AAPL$AAPL.Close
    
    ### Define UI for application
    ui <- fluidPage(
    
      # Sidebar panel
      sidebarPanel(
        selectInput("type",
          label = "1. Select plot type",
          choices = c("Line" = "line", "Dot" = "point", "Boxplot" = "box"),
          selected = 3
        )
      ),
    
      # Main Panel
      mainPanel(
        plotOutput("sp")
      )
    )
    
    server <- function(input, output) {
      output$sp <- renderPlot({
        colm <- as.numeric(apple)
        geom <- switch(input$type,
          line = geom_line(),
          point = geom_point(),
          box = geom_boxplot()
        )
        ggplot() +
          aes(x = 1:length(colm), y = colm) +
          geom +
          xlab(NULL)
      })
    }
    
    # Run the application
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      【解决方案2】:

      您可以使用match.fun 找到要应用的功能:

      library(shiny)
      library(quantmod)
      
      start <- as.Date("2013-01-01")
      end <- as.Date("2016-10-01")
      # Apple stock
      getSymbols("AAPL", src = "yahoo", from = start, to = end)
      apple <- AAPL$AAPL.Close
      
      ### Define UI for application
      ui <- fluidPage(
        # Sidebar panel
        sidebarPanel(
          selectInput("type",
                      label = "1. Select plot type",
                      choices = c("Line" = "geom_line", 
                                  "Dot" = "geom_point", "Boxplot" = "geom_boxplot"),
                      selected = 3
          )
        ),
        
        # Main Panel
        mainPanel(
          plotOutput("sp")
        )
      )
      
      server <- function(input, output) {
        output$sp <- renderPlot({
          colm <- as.numeric(apple)
          p <- ggplot() + aes(x = 1:length(colm), y = colm) + xlab(NULL)
          p + match.fun(input$type)()
            
        })
      }
      
      # Run the application
      shinyApp(ui = ui, server = server)
      

      【讨论】:

        猜你喜欢
        • 2012-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 2019-03-14
        相关资源
        最近更新 更多