2017-10-03 14 views
0

私の目標は、2つの棒グラフを持つ1つのプロットを作成することです.1つはフィルタリングされたデータセット、もう1つはフィルタリングされません。プロットを出力すると、ワイドバーが1つあります。 mysampleとmysample2を新しい変数に組み込み、それをx軸にプロットする必要があると私は信じています。誰かが別のアイデアを持っている場合は、私に知らせてください、私は本当にこれを行う方法に固執しています。 enter image description here2つのランダムサンプルで構成される新しい変数を作成する

また、私はx = input $ obsが完全に間違っていることを知っていますが、他に何を試していいのか分かりませんでした。

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

UI:

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)  
}) 
} 

答えて

2

。フィルタリングされたデータとフィルタされていないデータの両方を持つ単一のデータフレームが必要です。

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)  
}) 
} 
+0

ありがとう...、私はあなたのサーバ機能のため、このコードは動作すると思いますが、私はあなたがaes_string()を使用していた理由はわかりません!バーの1つが他のバーよりも高い。あなたは同じベースラインでそれらを得る方法を知っていますか? – Tarzan

+0

あなたはどのように見えるの画像を投稿できますか? –

+0

私はそれを持っていると思う:)あなたの助けに感謝 – Tarzan

関連する問題