R在“添加条形图”部分中,以绘图方式更改条形图的颜色

R在“添加条形图”部分中,以绘图方式更改条形图的颜色,r,plotly,r-plotly,R,Plotly,R Plotly,所以我在R中创建了一个曲线图,它有平均值、上置信度和下置信度的线。我为计数添加了“添加条”代码行。这就是我要找的。我可以更改线条的颜色,但不能更改条形图。这是我的代码: 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 Confid

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

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


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

您可以直接在
add_bar
中定义颜色,但应将其包装在
I()
表达式中,以查看输入的颜色。否则,plotly将尝试创建调色板并使用默认颜色。您还可以在
add_bar
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)))

使用类似于添加条形图(…,marker=list(颜色='rgb(0,0,0)')的方法来代替条形图=…
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)))