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

时间:2019-06-27 14:12:02

标签: r plotly r-plotly

我有一个组合的条形图和散点图,它几乎是完整的(见下文),但是我需要按其组(离群状态)自定义条形图颜色的帮助。基本上,我希望底部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))

1 个答案:

答案 0 :(得分:0)

您只需要向plot_ly()的{​​{1}}参数添加颜色矢量即可。

您可能想看看:

colors

通过它可以浏览plotly的可用迹线及其参数。


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

Result