The reason is that the input$x
and input$y
are character
class. So, instead of aes
, use aes_string
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")
})
A reproducible example with data(mpg)
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)
-output
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…