R Plotly : How to set the color of Individual Bars of a Waterfall Chart in R Plot. ly?

标签 r charts plotly waterfall

我正在尝试使用 R Plotly 更改瀑布图各个条的颜色。更具体地说,第一个和最后一个栏,我希望它分别是蓝色和黄色。所以在下图中,第 1 组必须是蓝色,第 2 组必须是黄色。

Waterfall Chart

R Plotly 瀑布图似乎只能选择为增加、减少和总条设置三种颜色。

这是用于生成上图的代码:

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 chart seems to only have the option to set three colors for increasing, decreasing and total bars.



不幸的是,你似乎完全正确。但好消息是,plotly 可以让您几乎随心所欲地向图表添加形状。您已经可以通过 totals 将一种您喜欢的颜色设置为 Group2颜色。所以你真的只需要添加一个形状就可以得到:

enter image description here

我不知道这是否适用于您的真实世界数据,但在这种情况下工作正常。这是你如何做到的:
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

你想要更多的形状吗?只需在 shapes 中添加另一个在 layout 中列出像这样:
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

输出:

enter image description here

关于R Plotly : How to set the color of Individual Bars of a Waterfall Chart in R Plot. ly?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58909584/

相关文章:

javascript - Google Charts - 仪表板 ComboChart 不堆叠条形图

java - 什么开源 Java 图表库可以在类别条形图中标记域?

r - 在 R plot_ly 中添加 boxplot 显着性指标线和星号

python - Plotly:如何为每个变量创建具有不同样式和颜色的线图?

r - 单步结合 tidyr::spread 和 dplyr::summarise

R - 创建一个单独的环境,其中 source() 一个 R 脚本,这样后者就不会影响 "caller"环境

javascript - 如何使用键盘监听器在调用时触发工具提示?

r - 使用 group_by 和 mutate 根据多个条件添加新列时出现意外的 '='

r - list.files考虑到R中的文件大小?

python - 绘制 yaxis2 手动缩放