R Plotly更改堆积条形图的颜色

时间:2017-09-13 16:37:15

标签: r plotly

我有一个Plotly堆积的条形图。我想改变条形的颜色,所以我有红色和深蓝色而不是你在下面看到的默认颜色

enter image description here

我的代码如下。我尝试使用marker函数,但它将整个绘图转换为单一颜色。任何帮助将不胜感激。

pie_subsegment_split %>%
plot_ly(x = ~Segment, y = ~Not*100, type = 'bar', name = 'Not') %>%
add_trace(y = ~QoL*100, name = 'QoL') %>%
layout(yaxis = list(title = 'Percentage (%)'),barmode = "stack",showlegend=T) %>%
layout(xaxis = list(title="Sub-Segment",showticklabels=FALSE))

1 个答案:

答案 0 :(得分:2)

marker按预期工作,您只需为每个跟踪指定它。我怀疑如果你只提供一种颜色,inherit可能会起作用。请尝试以下方法 -

library(plotly)
set.seed(1)
y1 <- runif(10, 5, 10)
set.seed(5)
y2 <- runif(10, 5, 10)
x <- 1:10
plot_ly(x = x, y = y1, type = 'bar', name = 'QoL', marker = list(color = 'red')) %>% add_trace(y = y2, name = 'not', marker = list(color = 'blue')) %>% layout(barmode = 'stack', yaxis = list(title = 'Percentage (%)'), xaxis = list(title = 'Sub-Segment', showticklabels = FALSE))

你应该得到以下 - enter image description here