2017-09-28 58 views
1

我幾乎完成了我的代碼,但我還有一個小問題。我需要根據下拉菜單中的輸入更改x和y軸。例如,如果x = Sales,則任何y = R & D,則x軸應該是「Sales(百萬美元)」,並且y軸應該是「R &D(百萬美元)」等等。但是y軸可以是Sales,x軸也可以是R & D,這讓我感到困惑。這是我到目前爲止的代碼:ggplot2多軸標題

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
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。請注意0​​中的更改selectInputlabs in ggplot

+0

哇謝謝!因此,當我這樣做時,它打印出來,例如,「SALEQ(百萬美元)」,但我希望它說「銷售」,而不是實際的變量名稱 – Tarzan

+0

@Tarzan查看我的編輯。由於你的問題是不可重複的,對於一個shinyApp來說這很公平是很難的,所以我無法測試它。所以請讓我知道是否還有其他問題。 – useR