按组自定义 plotly 条形图上的颜色

Customize colors on plotly bar chart by group

我有一个组合的条形图和散点图,它几乎已经完成(见下文),但我需要帮助来按组(离群值状态)自定义条形图的颜色。基本上,我希望底部 25% 的条形为红色,中间 50% 的条形为蓝色,顶部 25% 的条形为绿色。到目前为止,我只得到 R 的默认颜色。非常感谢任何帮助!

#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))

您只需将颜色向量添加到 plot_ly()s colors 参数。

您可能想看看:

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

让您浏览 plotly 的可用轨迹及其参数。


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))