使用R发光输入为ggplot轴划分列

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

我有以下代码,本质上是想接受两个输入,并返回一个绘图的出图,其中x轴上的时间与y轴上的时间为input1 / input2的比率。我曾尝试使用aes和aes_string,但似乎无法获得显示闪亮应用程序内部比率的工作块。 (将输入1与年份作图完全没有问题。

ui <- navbarPage("y",
  tabPanel('Teacher Type Comparison',
        sidebarLayout(
            sidebarPanel(
                selectInput('teacherInputOne', 'Compare ratio of',
                           choices = teacher_inputs, selected = 'male_total_staff'),
                selectInput('teacherInputTwo', 'to ratio of:',
                           choices = teacher_inputs,  selected = 'female_total_staff'),
                checkboxGroupInput('teacherState','States / Territories',area_names)
            ),

            mainPanel(
            plotOutput('teacherTypePlot'))
        )
    )


  )
server <- function(input, output) {

    output$teacherTypePlot <- renderPlot({
        ggplot(data = data, aes(x= year, y = (input$teacherInputOne / input$teacherInputTwo))) + geom_line(aes(colour = state))
    })}

shinyApp(ui = ui, server = server)
r ggplot2 shiny
1个回答
1
投票

我不确定我是否基于提供的代码完全理解了您的所有变量,但是我将更改服务器以根据UI中的输入选择来过滤数据,以将过滤后的值传递给Y aes。我只建议在服务器中更改代码:

    server <- function(input, output) {

      output$teacherTypePlot <- renderPlot({


        data <- data %>% 
          filter(var1 == input$teacherInputOne, 
                 var2 == input$teacherInputTwo)

        ggplot(data = data, aes(x= year, y = var3)) + 
          geom_line(aes(colour = state))
      })}
© www.soinside.com 2019 - 2024. All rights reserved.