2

I'm trying to incorporate stacked colors into a plotly grouped bar graph.

Take this example:

if (interactive()) {
  library(plotly)
  
  year <- c("2015", "2016", "2016", "2016", "2017", "2017", "2018", "2018", "2018", "2018")
  projectOwner <- c("Bob", "Larry", "Larry", "Bob", "Bob", "Bob", "Larry", "Bob", "Larry", "Larry")
  projectStatus <- c("Beginning", "Beginning", "Solved", "Solved", "Beginning", "Solved", "Beginning", "Solved", "Solved", "Beginning")
  numOfProjects <- c(1, 1, 1, 2, 1, 2, 2, 2, 2, 1)
  projectValue <- c("$500", "$1000" ,"$250", "$300", "$450", "$600", "$700", "$100", "$2000", "$450")
  
  data <- data.frame(year, projectOwner, projectStatus, numOfProjects, projectValue)
  
  ui <- fluidPage(
    plotlyOutput("projectPlot")
  )
  
  server <- function(input, output, session) {
    output$projectPlot <- renderPlotly({
      
      plot_ly(data,
              x = ~year,
              y = ~numOfProjects,
              type = "bar",
              name = ~projectStatus,
              hoverinfo = "text",
              hovertext = ~paste('Year:', year, '<br>',
                                 'Owner:', projectOwner, '<br>',
                                 'Status:', projectStatus, '<br>',
                                 'Projects:', numOfProjects, '<br>',
                                 'Value:', projectValue),
              textposition = 'auto',
              text = ~projectValue,
              showlegend = TRUE) %>%
        layout(titlefont = list(size = 15),
               margin = list(t = 50),
               xaxis = list(title=""),
               yaxis = list(title="", gridcolor = "gray"),
               barmode = 'group', legend = list(traceorder = 'normal')) %>%
        config(displaylogo=FALSE, collaborate = FALSE,
               modeBarButtonsToRemove = list("pan2d", "lasso2d", "zoomIn2d", "zoomOut2d", "hoverCompareCartesian")
        )
      
    })
  }
  shinyApp(ui, server)
}

Both Year 2016 and 2018 have stacked bars because of the different Project Owners, Bob and Larry for Solved projects. Similarly, there's stacked bars for each of them in 2018 for Beginning projects. Is there a way to make these different shades of orange and blue respectively to help differentiate the two owners, or at least make them different colors altogether and add them to the legend?

lumiukko
  • 249
  • 3
  • 13
  • The issue seems to be that you want to stack by status, and color (group) by status/owner. This is pretty hard to do in Plotly. Stacked and grouped barplots aren't really supported. Here is an example of how to sort-of hack it into working: https://stackoverflow.com/questions/64154396/combining-both-grouped-bar-chart-and-stacked-bar-chart-using-r-plotly/64156231 – BrianLang Dec 17 '20 at 14:18
  • Do you mean one continuous measure plotted on two different color gradients according to some group/factor? – AnilGoyal Dec 17 '20 at 16:12
  • @AnilGoyal Yeah, that's definitely a route I wouldn't be opposed to. – lumiukko Dec 17 '20 at 16:20
  • @BrianLang That solution looks like it would work if you have a defined number of Quarters/Years. But in my case, the Year range is dynamic so I wouldn't know if the user wanted to look at a single year vs multiple years. – lumiukko Dec 17 '20 at 16:25

0 Answers0