我正在尝试创建一个交互式闪亮仪表板,其中包含一个交互式绘图,您可以在其中更改绘图的值。我放在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")
})
答案 0 :(得分:1)
原因是input$x
和input$y
是character
类。因此,请使用aes
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")
})
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