2017-08-01 69 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