R 将颜色更改为 add_bars 部分中的条形图

R Plotly changing the color to the bars in the add_bars portion

所以我在 R 中创建了一个 Plotly 图,其中包含平均值、置信度上限和置信度下限的线条。我为计数添加了 "add_bars" 代码行。这就是我要找的。我可以更改线条的颜色,但不能更改条形图。这是我的代码:

plot_ly(df, x = ~Date) %>%
  add_lines(y = ~Mean, name = 'Mean', line = list(color = 'rgb(0, 0, 0)')) %>%
  add_lines(y = ~X95_upper_conf, name = 'Upper Confidence', line = list(color = 'rgb(205, 12, 24)', width = 4, dash = 'dot')) %>%
  add_lines(y = ~X95_lower_conf, name = 'Lower Confidence', line = list(color = 'rgb(205, 12, 24)', width = 4, dash = 'dot')) %>%
  add_bars(y = ~Count,name = 'Count', barplot = list(color = 'rgb(0, 0, 0)')) %>%
    layout(
      margin = list(b = 190, l = 50)) # to fully display the x and y axis labels

我试过添加:

barplot = list(color = 'rgb(0, 0, 0)')

line = list(color = 'rgb(0, 0, 0)')

但似乎都不起作用。任何帮助将不胜感激!

您可以直接在 add_bars 中定义颜色,但您应该将其包裹在 I() 表达式中,以查看您输入的颜色。否则 plotly 会尝试创建一个调色板并将使用默认颜色。您还可以在 add_bars.

marker 参数中定义颜色
library(plotly)

plot_ly(data=iris, x=~Species) %>% 
  add_lines(y = ~Sepal.Length) %>% 
  add_bars(y = ~Sepal.Width, color = I('red'))


plot_ly(data=iris, x=~Species) %>% 
  add_lines(y = ~Sepal.Length) %>% 
  add_bars(y = ~Sepal.Width, color = 'blue')

plot_ly(data=iris, x=~Species) %>% 
  add_lines(y = ~Sepal.Length) %>%
  add_bars(y = ~Sepal.Width, color = sample(c('yellow','red', 'blue'), 150, T))

plot_ly(data=iris, x=~Species) %>% 
  add_lines(y = ~Sepal.Length) %>% 
  add_bars(y = ~Sepal.Width, marker = list(color='rgb(0, 0, 0)'))


plot_ly(data=iris, x=~Species) %>% 
  add_lines(y = ~Sepal.Length) %>% 
  add_bars(y = ~Sepal.Width, marker = list(color="#4286f4"))


plot_ly(data=iris, x=~Species) %>% 
  add_lines(y = ~Sepal.Length) %>% 
  add_bars(y = ~Sepal.Width, color = rainbow(nrow(iris)))