2017-09-13 252 views
0

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

enter image description here

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

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

按预期工作,你只需要指定它每一道。如果您只提供一种颜色,我怀疑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

相关问题