在plotly R中添加边框到堆积条形图

时间:2018-04-17 01:50:18

标签: r graph border plotly

我在R中使用以下代码和plotly包来制作灰度堆叠条形图。

hplot1 = plot_ly(big, x=~Month, y=~Cyanophyta, type='bar', name='Cyanophyta', color=I("grey95")) %>%
       add_trace(y=~Chlorophyta, name='Chlorophyta', color=I("grey85")) %>%
       add_trace(y=~Bacillariophyta, name='Bacillariophyta', color=I("grey75")) %>%
       add_trace(y=~Dinophyta, name='Dinophyta', color=I("grey55")) %>%
       add_trace(y=~Cryptophyta, name='Cryptophyta', color=I("grey40")) %>%
       add_trace(y=~Other, name='Other', color=I("grey5")) %>%
       layout(title="2013 Phytoplankton Community", yaxis=list(title='Percent Abundance'), barmode='stack')

我不确定是否可以为每个单独的变量添加边界(即1,蓝藻周围1,绿藻门周围1等)可以这样做吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

通过添加

,可以将边框添加到堆叠条形图中,就像regular bar charts一样
marker = list(line = list(width = 5,
                          color = 'rgb(0, 0, 0)'))

add_trace

注意:此处必须color,否则该行将不可见。

library(plotly)

Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)

marker_style <- list(line = list(width = 5,
                                 color = 'rgb(0, 0, 0)'));

p <- plot_ly(data, type = 'bar') %>%
  add_trace(x = ~Animals, y = ~SF_Zoo, name = 'SF Zoo', 
            marker = marker_style) %>%
  add_trace(x = ~Animals, y = ~LA_Zoo, name = 'LA Zoo', 
            marker = marker_style) %>%
  layout(barmode = 'stack')

p

enter image description here