2017-08-01 60 views
-1

我有下面的代碼,它的設置使分配$ time「future」的任何行的alpha爲0.6,任何分配的$ time「past」的alpha都爲1。允許我的geom_bar中的值在我的「將來」數據中顯示爲稍微透明,並且對我的「過去」數據完全可靠。爲ggplot分配alpha值

但是,我的問題是,當我的輸入$ date_range在過去的兩個日期之間,我所有的geom_bars現在在0.6的alpha(代碼不是指定特定的alpha值到特定的$ time值,這是我想要的是)。

我試圖創建具有特定整數一個新的$阿爾法列用作阿爾法值但它只是做了我的「未來」的數據非常不透明,我不知道爲什麼...

allocated <- Project  Date  value  time 
       A   2017-05-15 4   past 
       B   2017-06-18 8   past 
       C   2017-07-25 3   past 
       D   2017-08-20 9   future 
       E   2017-09-14 4   future 



ui <- dashboardPage(
dashboardSidebar(
sidebarMenu(

    menuItem(
    dateRangeInput('date_range', label = "Date Range",format = "mm/dd/yyyy", start = Sys.Date()-17, end = Sys.Date()+17, startview = "month", weekstart = 0, separator = " to ", width = 200) 
) 
) 
), 

fluidRow(
    box(plotOutput("plot1", width = 1000, height = 500)) 
) 
) 


server <- function(input, output) { 

    output$plot1 <- renderPlot({ 

date_data <- reactive({ 
    subset(allocated, variable >= input$date_range[1] & variable <= input$date_range[2], value != 0) 
}) 



    ggplot(data = date_data(), aes(x = variable, y = value, alpha = time, fill = Project)) + 
    geom_bar(stat = 'identity') + 
    scale_alpha_discrete(range = c(0.6, 1), guide = 'none') 
    }) 
} 


shinyApp(ui, server) 
+2

嘗試一個**最小**可重現的例子。這聽起來不像問題與Shiny有什麼關係,所以擺脫所有的Shiny代碼。然後分享一些數據。 [參見這裏瞭解數據共享技巧(使用內置數據,共享代碼來模擬數據,或使用'dput()')](https://stackoverflow.com/questions/5963269/how-to-make-一個偉大-R重現-例子)。 「非常不透明」是什麼意思?正常,不透明? – Gregor

+0

@格雷戈謝謝你的評論,因爲你可能會告訴我是新來的網站。我更新了我的數據框的簡寫。然而,我保留了閃亮的代碼以保持內容不變。這更有幫助嗎?至於「extremeley不透明」,我的意思是酒吧出來幾乎完全透明,遠低於0.6設置,我試圖讓他們... – Naji

+0

這是一個改進。如果您的數據的代碼是複製/粘貼的,那將非常好。上面的鏈接和我的評論爲此提出了'dput'。 'dput(head(allocated))'是共享複製/可粘貼數據的一個很好的簡單方法。 (或一些其他子集,而不是'head(分配)',足以說明這個問題) – Gregor

回答

1

我很抱歉,我終於明白了。添加已分配的alpha數字的已分配$ alpha列,然後將scale_alpha_identity()添加到我的ggplot中,最終獲得了我想要的位置!

allocated <- Project  Date  value  time  alpha 
       A   2017-05-15 4   past  1 
       B   2017-06-18 8   past  1 
       C   2017-07-25 3   past  1 
       D   2017-08-20 9   future  0.6 
       E   2017-09-14 4   future  0.6 



ui <- dashboardPage(
dashboardSidebar(
sidebarMenu(

    menuItem(
    dateRangeInput('date_range', label = "Date Range",format = "mm/dd/yyyy", start = Sys.Date()-17, end = Sys.Date()+17, startview = "month", weekstart = 0, separator = " to ", width = 200) 
) 
) 
), 

fluidRow(
    box(plotOutput("plot1", width = 1000, height = 500)) 
) 
) 


server <- function(input, output) { 

    output$plot1 <- renderPlot({ 

date_data <- reactive({ 
    subset(allocated, variable >= input$date_range[1] & variable <= 
input$date_range[2], value != 0) 
}) 



    ggplot(data = date_data(), aes(x = variable, y = value, alpha = alpha, fill = Project)) + 
    geom_bar(stat = 'identity') + 
    scale_alpha_identity() 
    }) 
} 


shinyApp(ui, server) 
+0

好的工作計算出來! – Gregor