I am creating an R shiny dashboard and data is being filtered. Reactive data is processed. When different selections are made, the colors in the bar chart change and aren't consistent. The colors signify Good, Neutral, Bad, and not available so the order matters. Full shiny app is at: https://stoltzmaniac.shinyapps.io/medicare_data/ and the code is at: https://github.com/stoltzmaniac/Medicare/tree/master/shinyApp (both server.R and ui.R) Plotly portion looks like:
plot_ly(df_filtered, x = ~Score, y = ~Hospital.Name,
type = "bar", color = ~Compared.to.National, colors = FORMAT_CHART_COLOR_LIST,
hoverinfo = 'text', text = ~paste('State: ', State,
'<br> City: ', City,
'<br> Score: ', Score))
The plotly help says it here (scroll down a bit). Define a named color vector:
global_colors <- setNames(rainbow(nrow(data)), data$brand) # this maps brand to color
I suppose your data is
data <- head(mtcars)
data$brand <- rownames(data)
Then, the plot is created as
plot_ly(data, x = ~brand, y = ~hp, type = "bar", color = ~brand, colors = globalcolors)
(Note there's no ~
before globalcolors
because that's the global color vector)
Hope this helps :)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments