使用绘图不同的条之间的距离

时间:2019-11-18 18:44:06

标签: r plotly

我在rshiny中使用plotly创建了一个条形图。由于某些原因,第5条和第6条之间的空间要宽得多。不知道是什么原因造成了这个问题? 以下是在网页中浏览的该图表的输出。 enter image description here 下面是示例代码,供您参考。

# # Output - index option bar chart
  output$index_opt_bar <- renderPlotly({
    plot_ly(index_opt_bar_data(), x = 'SPX 1yCap', y = ~SPX_Annual_Cap_FV, type = 'bar', name = 'SPX 1yCap',
            text = ~paste(as.integer(100*SPX_Annual_Cap_FV/Total_FV),'%'), textposition = 'outside', textfont = list(color = '#000000'),
            marker = list(color = 'rgb(128,171,205)',line = list(color = '#000000', width = 1))) %>%
      add_trace( x = 'SPX 2yCap', y = ~SPX_Biannual_Cap_FV, type = 'bar', name = 'SPX 2yCap',
                 text = ~paste(as.integer(100*SPX_Biannual_Cap_FV/Total_FV),'%'), textposition = 'outside', textfont = list(color = '#000000'),
                 marker = list(color = 'rgb(252,168,128)',line = list(color = '#000000', width = 1))) %>%
      add_trace( x = 'SPX 1yBinary', y = ~SPX_Declared_FV, type = 'bar', name = 'SPX 1yBinary',
                 text = ~paste(as.integer(100*SPX_Declared_FV/Total_FV),'%'), textposition = 'outside', textfont = list(color = '#000000'),
                 marker = list(color = 'rgb(160,113,160)',line = list(color = '#000000', width = 1))) %>%
      add_trace( x = 'SPX 1yPar', y = ~SPX_Annual_Par_FV, type = 'bar', name = 'SPX 1yPar',
                 text = ~paste(as.integer(100*SPX_Annual_Par_FV/Total_FV),'%'), textposition = 'outside', textfont = list(color = '#000000'),
                 marker = list(color = 'rgb(246,203,105)',line = list(color = '#000000', width = 1))) %>%
      add_trace( x = 'SPX 1yEnPar', y = ~SPX_Annual_EnPar_FV, type = 'bar', name = 'SPX 1yEnPar',
                 text = ~paste(as.integer(100*SPX_Annual_EnPar_FV/Total_FV),'%'), textposition = 'outside', textfont = list(color = '#000000'),
                 marker = list(color = 'rgb(214,134,152)',line = list(color = '#000000', width = 1))) %>%
      add_trace( x = 'SPX 2yPar|2yEnPar|5yEnPar', y = ~SPX_Other_FV, type = 'bar', name = 'SPX 2yPar|2yEnPar|5yEnPar',
                 text = ~paste(as.integer(100*SPX_Other_FV/Total_FV),'%'), textposition = 'outside', textfont = list(color = '#000000'),
                 marker = list(color = 'rgb(255,222,220)',line = list(color = '#000000', width = 1))) %>%
      add_trace( x = 'EAFE & ACWI', y = ~INTL_All_FV, type = 'bar', name = 'EAFE & ACWI',
                 text = ~paste(as.integer(100*INTL_All_FV/Total_FV),'%'), textposition = 'outside', textfont = list(color = '#000000'),
                 marker = list(color = 'rgb(147,147,155)',line = list(color = '#000000', width = 1))) %>%
      add_trace( x = 'BEI', y = ~BEI_All_FV, type = 'bar', name = 'BEI',
                 text = ~paste(as.integer(100*BEI_All_FV/Total_FV),'%'), textposition = 'outside', textfont = list(color = '#000000'),
                 marker = list(color = 'rgb(151,232,212)',line = list(color = '#000000', width = 1))) %>%
      layout(title = "<b>Inforce Distribution<b>", titlefont = list(color = '#000000'), 
             paper_bgcolor = '#ffffff', plot_bgcolor = '#ffffff',
             xaxis = list(title = "", showticklabels = FALSE, 
                          categoryorder = "array", categoryarray = c('SPX 1yCap','SPX 2yCap','SPX 1yBinary','SPX 1yPar','SPX 1yEnPar','SPX 2yCap|2yEnPar|5yEnPar',"EAFE & ACWI", "BEI")),
             yaxis = list(title = "", color = '#000000',
                          showgrid = TRUE, gridcolor = '#000000'),
             legend = list(x = 0, y = -80, font = list(size = 10, color = '#000000'), orientation = 'h', traceorder = "normal"))
  })

非常感谢!

1 个答案:

答案 0 :(得分:2)

我想问题是您的categoryarray中的layout与您的数据不完全匹配。特别是,您在SPX 2yCap|2yEnPar|5yEnPar的{​​{1}}中有categoryarray,但是您对应的xaxisadd_trace。使用较长的变量名,可能很难在其中找到区别。

您可能已经注意到,您的图例与条形顺序匹配,除了此变量之外。变量SPX 2yPar|2yEnPar|5yEnPar位于末尾,空白处是错误标记的变量。如果未在数组中指定绘制的类别,则它将在SPX 2yPar|2yEnPar|5yEnPar中类别的末尾找到。

根据密谋reference

  

如果在categoryarray数组中找不到类别,则排序   该属性的行为将与“跟踪”模式相同。的   未指定的类别将遵循categoryarray中的类别。

为说明,请考虑以下示例:

categoryarray

plot with categories out of order