【问题标题】:Shiny: Change column used in ggplot2 dynamically闪亮:动态更改 ggplot2 中使用的列
【发布时间】:2016-07-18 08:15:37
【问题描述】:

我尝试创建一个闪亮的应用程序,您可以在其中为每个“selectizeInput”选择ggplot 的 x 轴。

我知道Gallery Example,这是通过预先选择所需的列来解决的。因为在我的情况下,我希望数据结构有点复杂,当可以动态更改aes() 中的x = 属性时。

为了更好地理解,我添加了一个最小的工作示例。不幸的是ggplot 使用输入作为值,而不是使用相应的列。

library(shiny)
library(ggplot2)


# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(

   # Application title
   titlePanel("Select x Axis"),       

   sidebarLayout(
      sidebarPanel(
        selectizeInput("xaxis", 
                       label = "x-Axis",
                       choices = c("carat", "depth", "table"))            
      ),          

      mainPanel(
         plotOutput("Plot")
      )
   )
))

server <- shinyServer(function(input, output) {

   output$Plot <- renderPlot({
     p <- ggplot(diamonds, aes(x = input$xaxis, y = price))
     p <-p + geom_point()
     print(p)
   })
})

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

【问题讨论】:

  • 使用aes_aes_string,参见?aes_。它会变成ggplot(diamonds, aes_string(x = input$xaxis, y = 'price'))
  • 完美运行,不知道aes_()。如果您将其发布为答案,我会将其标记为正确。谢谢。

标签: ggplot2 shiny


【解决方案1】:

aes 使用 NSE(非标准评估)。这非常适合交互式使用,但对于编程来说不是那么好。出于这个原因,有两个 SE(标准评估)替代方案,aes_(以前的aes_q)和aes_string。第一个采用带引号的输入,第二个是字符串输入。在这种情况下,使用aes_string 可以很容易地解决问题(因为selectizeInput 给了我们一个字符串)。

ggplot(diamonds, aes_string(x = input$xaxis, y = 'price')) +
  geom_point()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 2017-03-21
    • 2019-07-16
    相关资源
    最近更新 更多