-1

I have the following code and it's set up so that any rows with allocated$time "future" have an alpha of 0.6 and any with an allocated$time "past" have an alpha of 1. This allows values in my geom_bar to show as slightly transparent on my "future" data, and completely solid on my "past" data.

However my issue is that when my input$date_range is between two dates in the past, all of my geom_bars are now at an alpha of 0.6 (the code is not assigning specific alpha values to specific $time values, which is what I want).

I tried creating a new $alpha column with specific integers to be used as alpha values however it just made my "future" data extremely opaque and I'm not sure why...

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)
Naji
  • 674
  • 2
  • 14
  • 35
  • 2
    Try for a **minimal** reproducible example. It doesn't sound like the problem has anything to do with Shiny, so get rid of all the Shiny code. Then share some data. [See here for tips on data sharing (either use built-in data, share code to simulate data, or use `dput()`)](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). And what do you mean by "extremely opaque"? Just normal, not transparent? – Gregor Thomas Aug 01 '17 at 05:57
  • @Gregor thank you for the comment, as you might be able to tell I'm new to the site. I updated what I have with a shorthand of my data frame. I kept the shiny code to keep things in context, however. Is this more helpful? As for "extremeley opaque", what I meant was the bars come out almost entirely transparent, well under the 0.6 setting that I tried to keep them at... – Naji Aug 01 '17 at 17:29
  • It is an improvement. It would be really nice if the code for your data was copy/pasteable. The link and my comment above suggest `dput` for this. `dput(head(allocated))` is a nice easy way to share copy/pasteable data. (or some other subset than `head(allocated)`, enough to illustrate the problem) – Gregor Thomas Aug 01 '17 at 17:32
  • Speaking for myself, if you could isolate the problem to `ggplot` I might try to solve it - I'm good with ggplot and it would seem quick and easy to try to help you. If you leave it in Shiny it seems more complicated--I haven't used Shiny for over a year and it's not installed on my laptop--so I won't bother trying to solve it. Maybe someone else will, but you're limiting your pool of potential answerers to those who are good with both ggplot and Shiny. – Gregor Thomas Aug 01 '17 at 17:34
  • Besides that, the next debugging step is probably to determine whether the problem has something to do with Shiny or not - i.e., take the plot out of the Shiny app and see if the problem is still there. If the problem *is* still there, then the Shiny code is just a distraction, noise around the bug you're trying to fix. If the problem *isn't* still there, then the problem is with the Shiny part of the code, and you do need Shiny help. – Gregor Thomas Aug 01 '17 at 17:38
  • 1
    @Gregor thank you so much for the advice. I posted an answer after finally figuring out how to fix my issue but I will definitely make sure I follow what you're saying as I continue to contribute to this forum. As a new developer I'm trying to learn how to properly collaborate with others so I really do appreciate the help and you taking the time to consider my question :) – Naji Aug 01 '17 at 17:45

1 Answers1

4

I'm so sorry, I finally figured it out. adding an allocated$alpha column with assigned alpha numbers, and then adding scale_alpha_identity() to my ggplot finally got me where I wanted!

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)
Naji
  • 674
  • 2
  • 14
  • 35