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?