R-如何使用selectInput in Shining更改ggplot renderPlot中的x和填充变量?

R-如何使用selectInput in Shining更改ggplot renderPlot中的x和填充变量?,r,ggplot2,shiny,R,Ggplot2,Shiny,我正在尝试制作一个交互式闪亮仪表板,它有一个交互式绘图,您可以在其中更改绘图的值。我放在renderPlot中的代码块工作正常,所以我不明白为什么当我使用selectInput更改X和Fill变量时,count没有显示在y轴上 inputPanel( selectInput('x', 'X', names(data)), selectInput('y', 'Y', names(data)) ) renderPlot({ ggplot(data, aes(x = input$x

我正在尝试制作一个交互式闪亮仪表板,它有一个交互式绘图,您可以在其中更改绘图的值。我放在renderPlot中的代码块工作正常,所以我不明白为什么当我使用selectInput更改X和Fill变量时,count没有显示在y轴上

 inputPanel(
  selectInput('x', 'X', names(data)),
  selectInput('y', 'Y', names(data))
)

renderPlot({
    ggplot(data, aes(x = input$x)) +
  geom_bar(aes(fill = input$y), position = position_stack(reverse = TRUE)) +
 coord_flip() + 
 theme(legend.position = "top")
})

原因是
input$x
input$y
character
类。因此,使用
aes\u字符串来代替
aes

renderPlot({
  ggplot(data, aes_string(x = input$x)) +
  geom_bar(aes_string(fill = input$y), position = position_stack(reverse = TRUE)) +
  coord_flip() + 
  theme(legend.position = "top")
})

具有
数据(mpg)

库(闪亮)
图书馆(GG2)
数据(mpg)

非常感谢你!这起作用了。我被困了几个小时;-;您知道如何使selectinput成为列的因子级别,而不仅仅是列吗?@ThomasKidd在这种情况下,您可能必须使用
级别(数据[,列])
。不过这个问题还不清楚。你能发一个新的邮件吗question@akrun如果这将用于多个绘图,是否有动机将列选择的inout分配给全局
reactiveVal
?例如,
globalXY
library(shiny)
library(ggplot2)


data(mpg)

ui <- fluidPage(
  inputPanel(
    selectInput('x', 'X', choices = c("manufacturer", "model", "year", "cyl", "class"),
          selected = "class"),
    selectInput('y', 'Y', choices = c( "trans", "fl", "drv"), 
  selected = "drv")
  ),

  mainPanel(plotOutput("outplot"))

)

server <- function(input, output) {

  output$outplot <- renderPlot({
    ggplot(mpg, aes_string(x = input$x)) +
      geom_bar(aes_string(fill= input$y), position = position_stack(reverse = TRUE)) +
      coord_flip() + 
      theme(legend.position = "top")
  })

}

shinyApp(ui = ui, server = server)