简体   繁体   English

R绘制更改堆积条形图的颜色

[英]R Plotly change color of stacked bar chart

I have a Plotly stacked bar plot. 我有一个Plotly堆积的条形图。 I'd like to change the colors of the bars so I have Red and Dark Blue instead of the default colors you see below 我想更改条形的颜色,所以我有红色和深蓝色,而不是下面看到的默认颜色

在此处输入图片说明

My code below. 我的代码如下。 I tried using the marker function but it converts the whole plot to a single color. 我尝试使用marker功能,但是它将整个图转换为单一颜色。 Any help would be appreciated. 任何帮助,将不胜感激。

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))

marker works as expected, you only need to specify it for each trace. marker可以按预期工作,只需为每个跟踪指定它。 I suspect inherit ance might be at play if you provide only one colour. 我怀疑如果您仅提供一种颜色,则inherit可能会发挥作用。 Try the following - 试试以下-

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))

You should get the following - 您应该获得以下内容- 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM