Reputation: 33
I created a bar chart using plotly in rshiny. For some reason, there is a much wider space in between the 5th and the 6th bars. Wonder what caused this issue?
Below is the output of this chart browsed in webpage.
Below is the sample code for your reference.
# # 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"))
})
Much thanks!
Upvotes: 1
Views: 604
Reputation: 30474
I would guess that the problem is that your categoryarray
in your layout
does not exactly match your data. In particular, you have SPX 2yCap|2yEnPar|5yEnPar
in the categoryarray
for your xaxis
but your corresponding add_trace
has SPX 2yPar|2yEnPar|5yEnPar
. With the longer variable name it may have been hard to catch the difference there.
You may have noticed that your legend matches the order of bars, except for this variable. The variable SPX 2yPar|2yEnPar|5yEnPar
is at the end, and an empty space for the mislabeled variable. If a category plotted is not specified in the array, it will be found at the end of the categories in categoryarray
.
According to the Plotly reference:
If a category is not found in the
categoryarray
array, the sorting behavior for that attribute will be identical to the "trace" mode. The unspecified categories will follow the categories incategoryarray
.
To illustrate, consider the example below:
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
Upvotes: 2