ggplot2多轴标题

时间:2017-09-28 16:56:06

标签: r ggplot2 rstudio shiny

我差不多完成了我的代码,但我还有一个小问题。我需要根据下拉菜单中的输入更改x和y轴。例如,如果x = Sales,任何y = R& D,则x轴应为" Sales(数百万美元)"并且y轴应该是" R& D(数百万美元),"等等。但是y轴可以是Sales,x轴也可以是R& D,这让我感到困惑。到目前为止,这是我的代码:

UI:

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)

1 个答案:

答案 0 :(得分: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$xinput$y都是字符串,因此您只需添加

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

修改

OP提出了一个观点,即希望使用名称,因为它们在用户看来是轴标签而不是变量名。由于需要值和名称,因此可以创建标签选择的全局变量,按原样提供给shinyUI,并将names提供给匹配input$xinput$y的shinyServer。请注意choice =selectInputlabsggplot的更改