【问题标题】:ggplot2 multiple axis titlesggplot2 多轴标题
【发布时间】:2017-09-28 16:56:06
【问题描述】:

我几乎完成了我的代码,但我还有一个小问题。我需要根据下拉菜单中的输入更改 x 和 y 轴。例如,如果 x = 销售额,任何 y = 研发,则 x 轴应为“销售额(百万美元)”,y 轴应为“研发(百万美元)”,依此类推。但是y轴可以是销售,x轴也可以是研发,这让我很困惑。到目前为止,这是我的代码:

用户界面:

ui = dashboardPage(
  dashboardHeader(title = "Apple Financials"),
  dashboardSidebar(
    fileInput("file1", label = "Upload SAS Data:", accept = ".sas7bdat"),
    selectInput("x", label = "X-Axis Variable", choices = c("Sales" = "SALEQ", "Cash" = "CHEQ", "Assets" = "ATQ", "Profits" = "OIADPQ", "R&D" = "XRDQ", "SG&A" = "XSGAQ")),
    selectInput("y", label = "Y-Axis Variable", choices = c("Sales" = "SALEQ", "Cash" = "CHEQ", "Assets" = "ATQ", "Profits" = "OIADPQ", "R&D" = "XRDQ", "SG&A" = "XSGAQ"), selected = "XRDQ"),
    selectInput("scale", label = "Choose the Scale:", choices = c("Levels" = "identity", "Log 10" = "log10")),
    radioButtons("model", label = "Choose the Model:", choices = c("Linear Model" = "lm", "LOESS" = "loess", "Robust Linear" = "rlm", "None"), selected = "loess"),
    checkboxInput("ribbon", label = "Standard Error Ribbon", value = TRUE),
    conditionalPanel(
      condition = "input.model == 'loess'",
      sliderInput("span", label = "Span for LOESS", min = 0, max = 1, value = .75)
    )
  ),
  dashboardBody(
    box(width = NULL, height = 415, plotOutput("plots"))
  )
)

服务器:

  server = function(input, output) {

 output$plots = renderPlot({
   data = input$file1
   if(is.null(data))
     return(NULL)
  df = read_sas(data$datapath)

 ggplot(df, aes_string(x = input$x, y = input$y)) +
   geom_point(size = 2) +
   geom_smooth(method = input$model, span = input$span, se =                           
    input$ribbon) +
   scale_x_continuous(trans = input$scale) +
   scale_y_continuous(trans = input$scale) +
   theme_minimal() +
   validate(
     need(input$x != input$y,
          paste("X and Y variables have to be different"))
     )
   })
  }


  shinyApp(ui, server)

【问题讨论】:

    标签: r ggplot2 rstudio shiny


    【解决方案1】:
    lab_choices = c("Sales" = "SALEQ", 
                    "Cash" = "CHEQ", 
                    "Assets" = "ATQ", 
                    "Profits" = "OIADPQ", 
                    "R&D" = "XRDQ", 
                    "SG&A" = "XSGAQ")
    
    ui = dashboardPage(
      dashboardHeader(title = "Apple Financials"),
      dashboardSidebar(
        fileInput("file1", label = "Upload SAS Data:", accept = ".sas7bdat"),
        selectInput("x", label = "X-Axis Variable", choices = lab_choices),
        selectInput("y", label = "Y-Axis Variable", choices = lab_choices, selected = "XRDQ"),
        selectInput("scale", label = "Choose the Scale:", choices = c("Levels" = "identity", "Log 10" = "log10")),
        radioButtons("model", label = "Choose the Model:", choices = c("Linear Model" = "lm", "LOESS" = "loess", "Robust Linear" = "rlm", "None"), selected = "loess"),
        checkboxInput("ribbon", label = "Standard Error Ribbon", value = TRUE),
        conditionalPanel(
          condition = "input.model == 'loess'",
          sliderInput("span", label = "Span for LOESS", min = 0, max = 1, value = .75)
        )
      ),
      dashboardBody(
        box(width = NULL, height = 415, plotOutput("plots"))
      )
    )
    
    server = function(input, output) {
    
     output$plots = renderPlot({
       data = input$file1
       if(is.null(data))
         return(NULL)
      df = read_sas(data$datapath)
    
     ggplot(df, aes_string(x = input$x, y = input$y)) +
       geom_point(size = 2) +
       geom_smooth(method = input$model, span = input$span, se =                           
        input$ribbon) +
       labs(x = paste(names(lab_choices)[lab_choices == input$x], "(millions $)"), 
            y = paste(names(lab_choices)[lab_choices == input$y], "(millions $)")) +
       scale_x_continuous(trans = input$scale) +
       scale_y_continuous(trans = input$scale) +
       theme_minimal() +
       validate(
         need(input$x != input$y,
              paste("X and Y variables have to be different"))
         )
       })
    }
    

    由于input$x 和input$y 都是字符串,你可以添加

    labs(x = paste(input$x, "(millions $)"), 
         y = paste(input$y, "(millions $)"))
    

    编辑:

    OP 提出要使用在用户看来的名称作为轴标签而不是变量名称。由于需要值和名称,因此可以创建标签选择的全局变量,按原样提供给 shinyUI,并将names 提供给匹配input$x 和input$y 的 shinyServer。注意selectInput 中choice = 和ggplot 中labs 的更改

    【讨论】:

    • 哇,谢谢!因此,当我这样做时,它会打印出例如“SALEQ(百万美元)”,但我希望它说“Sales”而不是实际的变量名
    • @Tarzan 查看我的编辑。由于您的问题不可重现,公平地说,这对于 shinyApp 来说很难,我无法对其进行测试。因此,如果还有其他问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 2013-02-03
    • 2018-01-31
    相关资源
    最近更新 更多