首页 > 解决方案 > 使用绘图的条形之间的不同距离

问题描述

我在 rshiny 中使用 plotly 创建了一个条形图。出于某种原因,第 5 和第 6 小节之间的空间要宽得多。想知道是什么导致了这个问题?以下是在网页中浏览的此图表的输出。 在此处输入图像描述 以下是供您参考的示例代码。

# # 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"))
  })

非常感谢!

标签: rplotly

解决方案


我猜问题是您categoryarraylayout数据与您的数据不完全匹配。特别是,你有SPX 2yCap|2yEnPar|5yEnParfor categoryarrayyourxaxis但你对应的add_tracehas SPX 2yPar|2yEnPar|5yEnPar。使用较长的变量名,可能很难发现那里的差异。

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

根据情节参考

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

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

library(plotly)

df <- data.frame(
  count = c(1, 2, 3, 4), 
  names = c("First", "Second", "Third", "Fourth")
)

p <- plot_ly(data = df, x = ~names, y = ~count, type = "bar")
p <- layout(p, xaxis = list(categoryarray = c("First", "Second", "Somethingelse", "Fourth"), categoryorder = "array"))
p

类别乱序的绘图


推荐阅读