繁体   English   中英

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

[英]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()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))

结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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