R Plotly:如何在R Plot.ly中设置瀑布图的单个条的颜色? [英] R Plotly: How to set the color of Individual Bars of a Waterfall Chart in R Plot.ly?

查看:153
本文介绍了R Plotly:如何在R Plot.ly中设置瀑布图的单个条的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R Plotly更改瀑布图的各个条形的颜色.更具体地说,第一个和最后一个栏分别是蓝色和黄色.因此,在下图中,组1必须为蓝色,组2必须为黄色.

I am trying to change the color of the individual bars of a waterfall chart using R Plotly. More specifically the first and the last bar, I want it to be blue and yellow respectively. So in the below graph, Group 1 has to be blue and Group 2 has to be yellow.

R Plotly Waterfall图表似乎只能选择设置三种颜色来增加,减少和总计柱线.

R Plotly Waterfall chart seems to only have the option to set three colors for increasing, decreasing and total bars.

以下是用于生成以上图形的代码:

Here is the code used to generate the above graph:

library(plotly)

df <- data.frame(Rank = 1:7, Variable = c("Group 1","A","B","C","D","E","Group 2"),
Value = c(10,2,2,2,1,1,20),
measure = c("relative","relative","relative","relative","relative","relative","total"),
colour = c("yellow","green","green","green","green","green","blue"))


df$Variable <- factor(df$Variable, levels = unique(df$Variable))
df$text <- as.character(round(df$Value,2))
df$Factor <- as.numeric(df$Variable)



plot_ly(df, name = "20", type = "waterfall", measure = ~measure,
             x = ~Variable, textposition = "outside", y= ~Value, text =~paste0('$',text),
             hoverinfo='none',cliponaxis = FALSE,
             connector = list(line = list(color= "rgb(63, 63, 63)"))
        ) %>%
  layout(title = "",
         xaxis = list(title = ""),
         yaxis = list(title = "",tickprefix ='$'),
         autosize = TRUE,
         showlegend = FALSE
         )

任何帮助将不胜感激.

推荐答案

R Plotly Waterfall图表似乎只能选择设置三个用于增加,减少和总条形的颜色.

R Plotly Waterfall chart seems to only have the option to set three colors for increasing, decreasing and total bars.

Unfortunaley,您似乎是完全正确的.但是好消息是,plotly使您几乎可以根据需要向图形添加形状.现在,您已经可以通过 totals 颜色将首选颜色之一设置为Group2.因此,您只需要添加一种形状即可获得:

Unfortunaley, you seem to be absolutely right. But the good news is that plotly lets you add shapes to charts almost just as you'd like. And you're already able to set one of your preferred colors to Group2 via the totals color. So you only really need to add one shape to get:

我不知道您的现实世界数据是否可行,但是在这种情况下可以正常工作.操作方法如下:

I don't know if this will be possible to your real world data, but in works fine in this case. Here's how you do it:

library(plotly)

df <- data.frame(Rank = 1:7, Variable = c("Group 1","A","B","C","D","E","Group 2"),
Value = c(10,2,2,2,1,1,20),
measure = c("relative","relative","relative","relative","relative","relative","total"),
colour = c("yellow","green","green","green","green","green","yellow"))


df$Variable <- factor(df$Variable, levels = unique(df$Variable))
df$text <- as.character(round(df$Value,2))
df$Factor <- as.numeric(df$Variable)



p<-plot_ly(df, name = "20", type = "waterfall", measure = ~measure,
             x = ~Variable, textposition = "outside", y= ~Value, text =~paste0('$',text),
             hoverinfo='none',cliponaxis = FALSE,
             connector = list(line = list(color= "rgb(63, 63, 63)")),
             totals = list(marker = list(color = "yellow", line = list(color = 'yellow', width = 3)))
        ) %>%
  layout(title = "",
         xaxis = list(title = ""),
         yaxis = list(title = "",tickprefix ='$'),
         autosize = TRUE,
         showlegend = FALSE,


         shapes = list(
               list(type = "rect",
                    fillcolor = "blue", line = list(color = "blue"), opacity = 1,
                    x0 = -0.4, x1 = 0.4, xref = "x",
                    y0 = 0.0, y1 = 10, yref = "y"))

         )
p

您想要更多形状吗?只需在 layout shapes 列表中添加另一个,就像这样:

Would you like more shapes? Just add another in the shapes list in layout like this:

library(plotly)

df <- data.frame(Rank = 1:7, Variable = c("Group 1","A","B","C","D","E","Group 2"),
Value = c(10,2,2,2,1,1,20),
measure = c("relative","relative","relative","relative","relative","relative","total"),
colour = c("yellow","green","green","green","green","green","yellow"))


df$Variable <- factor(df$Variable, levels = unique(df$Variable))
df$text <- as.character(round(df$Value,2))
df$Factor <- as.numeric(df$Variable)



p<-plot_ly(df, name = "20", type = "waterfall", measure = ~measure,
             x = ~Variable, textposition = "outside", y= ~Value, text =~paste0('$',text),
             hoverinfo='none',cliponaxis = FALSE,
             connector = list(line = list(color= "rgb(63, 63, 63)")),
             totals = list(marker = list(color = "yellow", line = list(color = 'yellow', width = 3)))
        ) %>%
  layout(title = "",
         xaxis = list(title = ""),
         yaxis = list(title = "",tickprefix ='$'),
         autosize = TRUE,
         showlegend = FALSE,


         shapes = list(
               list(type = "rect",
                    fillcolor = "blue", line = list(color = "blue"), opacity = 1,
                    x0 = -0.4, x1 = 0.4, xref = "x",
                    y0 = 0.0, y1 = 10, yref = "y"),

               list(type = "rect",
                 fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
                 x0 = 3, x1 = 4, xref = "x",
                 y0 = 4, y1 = 12.5, yref = "y"))




         )
p

输出:

这篇关于R Plotly:如何在R Plot.ly中设置瀑布图的单个条的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆