r - 使用绘图的条形之间的不同距离
问题描述
我在 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"))
})
非常感谢!
解决方案
我猜问题是您categoryarray
的layout
数据与您的数据不完全匹配。特别是,你有SPX 2yCap|2yEnPar|5yEnPar
for categoryarray
yourxaxis
但你对应的add_trace
has 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
推荐阅读
- php - php-输出目录不存在
- java - Firebase 谷歌身份验证两步
- laravel - 从 laravel 下载 Dropbox 文件的问题
- regex - 正则表达式:从子字符串中获取变量
- c - 我的 if 和 else if 同时运行
- database - 为服务的最终用户提供伪造数据保护——工具?
- php - 降序在查询中不起作用,但它应该起作用吗?
- android - Shopify 与 Apollo 客户端“无法解析 http 响应”
- jquery - 引导下拉菜单不起作用,可能是 jquery 和 webpacker 之间的冲突?
- parallel-processing - 一般来说,就 Apache Beam 和并行处理而言,“交换和关联”是什么意思?