取决于选择输入的R ggplot facet_wrap

问题描述 投票:0回答:1

我正在尝试制作R shiny应用程序,该应用程序允许您选择一个组(性别,年龄,种族等),然后在该组中每个级别的facet_wrap中显示风险得分的直方图。例如,如果选择性别作为组,则直方图将具有针对男性和女性的方面。在下面的代码中,它不会产生任何方面。

library(shiny)
library(ggplot2)
# Define UI for miles per gallon app ----
ui <- fluidPage(

  # Application title
  titlePanel("Group fairness analysis"),

  # Sidebar 
  sidebarLayout(
    sidebarPanel(
      selectInput("group", "Group:", 
                  c("Age" = "age",
                    "Gender" = "gender",
                    "Region" = "region",
                    "Ethnicity"="ethnicity"))
      ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# Define server logic----
server <- function(input, output) {

  output$distPlot <- renderPlot({   
  gg <- ggplot(df, aes(x=score))+
      geom_histogram(breaks=seq(0,100,10))+
      facet_wrap(~input$group)
      gg

  })

}

shinyApp(ui, server)
r ggplot2 shiny facet-wrap
1个回答
1
投票

因为input$group是字符类型,所以不起作用,而facet_wrap需要带引号的变量。]​​>

通过用get引用来简单地解决它:

gg <- ggplot(df, aes(x=score))+
      geom_histogram(breaks=seq(0,100,10))+
      facet_wrap(~get(input$group))

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.