Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R Plot.ly:如何在R Plot.ly中设置瀑布图各个条形的颜色?_R_Charts_Plotly_Waterfall - Fatal编程技术网

R Plot.ly:如何在R Plot.ly中设置瀑布图各个条形的颜色?

R Plot.ly:如何在R Plot.ly中设置瀑布图各个条形的颜色?,r,charts,plotly,waterfall,R,Charts,Plotly,Waterfall,我正在尝试使用R Plotly更改瀑布图各个条形图的颜色。更具体地说,第一个和最后一个条,我希望它分别是蓝色和黄色。因此,在下图中,第1组必须为蓝色,第2组必须为黄色 R Plotly瀑布图似乎只能为增加、减少和总条形设置三种颜色 以下是用于生成上述图形的代码: library(plotly) df <- data.frame(Rank = 1:7, Variable = c("Group 1","A","B","C","D","E","Group 2"), Value = c(10,

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

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
         )
library(plotly)
df
R Plotly瀑布图似乎只有三个选项
用于增加、减少和合计条的颜色

不幸的是,你似乎完全正确。但好消息是plotly可以让您几乎按照自己的喜好向图表中添加形状。您已经可以通过
总计
颜色将一种首选颜色设置为Group2。因此,您只需添加一个形状即可获得:

我不知道这是否可能应用于您的真实世界数据,但在这种情况下效果很好。以下是您的操作方法:

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
输出:


正是我想要的。非常感谢。我也有同样的问题,但我正在使用Python。我搜索了,但在API中找不到
形状
选项。先生,你介意看一看吗?@RodrikTheReader你找到解决办法了吗?@vestland不幸的是,没有。@RodrikTheReader如果你把它作为一个问题发布,我会看一看。
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