代码之家  ›  专栏  ›  技术社区  ›  Tarzan

创建由2个随机样本组成的新变量

  •  0
  • Tarzan  · 技术社区  · 8 年前

    我的目标是绘制一个具有2条线的图:1条用于过滤数据集,1条用于未过滤数据集。当我输出绘图时,我有一个宽条。我认为我需要将mysample和mysample2组合成一个新变量,并在x轴上绘制它。如果有人有不同的想法,请随时告诉我,我真的被困在如何做到这一点。 enter image description here

    此外,我知道x=input$obs完全错误,但我不知道还能尝试什么。

    filtered = readxl::read_excel("/Filter.xlsx")
    unfiltered = readxl::read_excel("/Unfilter.xlsx")
    

    ui = fluidPage(
      sliderInput("obs", "Number of Observations", value = 550, min = 100, max = 1000),
      plotOutput("filter")
    )  
    

    服务器:

    server = function(input, output) {
      output$filter = renderPlot({
        mysample = filtered[sample(1:nrow(filtered), input$obs,
                                replace=FALSE),]
         mysample2 = unfiltered[sample(1:nrow(unfiltered), input$obs,
                                replace=FALSE),]
    
    
      ggplot(NULL, aes_string(x = input$obs)) +
        geom_col(data = mysample, aes(y = Net_Return)) +
        geom_col(data = mysample2, aes(y = Net_Return)) +
    
        labs(y = "Net Return") +
        theme_bw() +
        scale_y_continuous(labels = scales::dollar)     
     })
    }  
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Alex P    8 年前

    我认为你是对的;您需要一个包含过滤和未过滤数据的单个数据帧。我认为这段代码可以用于你的服务器功能,但我不确定你为什么要使用它 aes_string()

    server = function(input, output) {
      output$filter = renderPlot({
        mysample = filtered[sample(1:nrow(filtered), input$obs,
                                replace=FALSE),]
         mysample2 = unfiltered[sample(1:nrow(unfiltered), input$obs,
                                replace=FALSE),]
        tbl = bind_rows(filtered = mysample, unfiltered = mysample2,
         .id="type")
    
    
      ggplot(tbl, aes(x = type)) +
        geom_col(aes(y = `Net Return`)) +
        labs(y = "Net Return") +
        theme_bw() +
        scale_y_continuous(labels = scales::dollar)     
     })
    }  
    
    推荐文章