簡體   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