将 alpha 值分配给 ggplot

Assigning alpha values to ggplot

我有以下代码,它被设置为任何分配了 $time "future" 的行的 alpha 为 0.6,任何分配了 $time "past" 的行的 alpha 为 1。这允许我的 geom_bar 中的值在我的 "future" 数据上显示为略微透明,并且在我的 "past" 数据上完全固定。

但是我的问题是,当我的输入 $date_range 在过去的两个日期之间时,我所有的 geom_bar 现在都处于 0.6 的 alpha(代码没有指定具体alpha 值到特定的 $time 值,这就是我想要的)。

我尝试创建一个新的 $alpha 列,其中包含用作 alpha 值的特定整数,但这只会让我的 "future" 数据变得非常不透明,我不确定为什么...

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)

对不起,我终于想通了。添加一个 allocated$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)