简体   繁体   中英

Customize colors on plotly bar chart by group

I have a combined bar and scatter plotly graph which is almost complete (see below) but I need assistance with customizing the colors of the bar chart by their group (Outlier Status). Basically, I would want bars in the Bottom 25% to be red, the bars in the Middle 50% to be blue, and the bars in the Top 25% to be green. So far I only get R's default colors. Any help is much appreciated!

#Create dataset
ho_graph1 = data.frame(
   "Code" = c("G","L","K","I","B","N","O","M","F","D","H","C","J"),
   "Rate" = c(600, 550, 500, 450, 400, 350, 300, 250, 200, 150, 100, 50, 5),
   "AUR" = c(2.8, 2.6, 2.4, 1.5, 2.1, 1.6, 1.4, 1.3, 1.1, 0.8, 0.5, 0.3, 
0.7),
   "Outlier Status" = c(rep("Bottom 25%", times=4), rep("Middle 50%", 
times=6), rep("Top 25%", times=3)))

#Sort by decreasing rate
ho_graph1$Code <- factor(ho_graph1$Code, levels = unique(ho_graph1$Code) 
   [order(ho_graph1$Rate, decreasing = TRUE)])

#Graph
plot_ly(ho_graph1, type = 'bar', x = ~Code, y = ~Rate, color = 
~Outlier.Status, legendgroup = ~Outlier.Status,
hoverinfo = "text", text = ~paste('BS-HO Prescribing Rate: ', Rate, "\n", 
  'Provider: ', Code)) %>% 
add_trace(y = ~AUR, type = 'scatter', mode='markers', yaxis = 'y2', 
    showlegend = FALSE,
    marker = list(size = 13,
    color = 'rgb(240,230,140)',
    line = list(color = 'rgb(255,215,0)',
      width = 2)),
    hoverinfo = "text",
    text = ~paste('O:E: ', AUR, "\n", 'Provider: ', Code)) %>%
layout(title = 'BS-HO Prescribing Rate and O:E by Provider, Mar-Apr 
2019',
    xaxis = list(title = ""),
    yaxis = list(side = 'left', title = 'BS-HO Prescribing Rate', showgrid = 
      FALSE, zeroline = FALSE),
    yaxis2 = list(side = 'right', overlaying = "y", title = 'O:E', showgrid 
       =  FALSE, zeroline = FALSE))

You just need to add a color vector to plot_ly() s colors argument.

You might want to have a look at:

library(plotly)
library(listviewer)
schema(jsonedit = interactive())

which let's you navigate through plotly's available traces and their arguments.


library(plotly)

#Create dataset
ho_graph1 = data.frame(
  "Code" = c("G","L","K","I","B","N","O","M","F","D","H","C","J"),
  "Rate" = c(600, 550, 500, 450, 400, 350, 300, 250, 200, 150, 100, 50, 5),
  "AUR" = c(2.8, 2.6, 2, 1.5, 2.1, 1.6, 1.4, 1.3, 1.1, 0.8, 0.5, 0.3, 
            0.7),
  "Outlier Status" = c(rep("Bottom 25%", times=4), rep("Middle 50%", 
                                                       times=6), rep("Top 25%", times=3)))

#Sort by decreasing rate
ho_graph1$Code <- factor(ho_graph1$Code, levels = unique(ho_graph1$Code) 
                         [order(ho_graph1$Rate, decreasing = TRUE)])

#Graph
plot_ly(ho_graph1, type = 'bar', x = ~Code, y = ~Rate, color = 
          ~Outlier.Status, colors = c("red", "blue", "chartreuse3"), legendgroup = ~Outlier.Status,
        hoverinfo = "text", text = ~paste('BS-HO Prescribing Rate: ', Rate, "\n", 
                                          'Provider: ', Code)) %>% 
  add_trace(y = ~AUR, type = 'scatter', mode='markers', yaxis = 'y2', 
            showlegend = FALSE,
            marker = list(size = 13,
                          color = 'rgb(240,230,140)',
                          line = list(color = 'rgb(255,215,0)',
                                      width = 2)),
            hoverinfo = "text",
            text = ~paste('O:E: ', AUR, "\n", 'Provider: ', Code)) %>%
  layout(title = 'BS-HO Prescribing Rate and O:E by Provider, Mar-Apr 
2019',
         xaxis = list(title = ""),
         yaxis = list(side = 'left', title = 'BS-HO Prescribing Rate', showgrid = 
                        FALSE, zeroline = FALSE),
         yaxis2 = list(side = 'right', overlaying = "y", title = 'O:E', showgrid 
                       =  FALSE, zeroline = FALSE))

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM