How to create data labels for grouped bar chart in R

Hi All,

I’ve created a grouped bar chart from tidy data, but am struggling with creating data labels for each individual bar within a group. All of the data labels seem to be at the correct x axis position, however, they are inline with the mid-point of each group.

This is my output,

I’ve tried changing the values for xref, x, xanchor, yref, y, and yanchor, however, have not seen the result I’d like, and have referenced, @Justin_Meyer 's, post, as this is the same question.

This is my current code,

p <- credInput() %>%
  group_by(CredentialQ) %>%
  plot_ly(x = ~vizCredPrcnt,
          y = ~forcats::fct_reorder(CredentialQ, group_vizCredPrcntMean),
          color = ~factor(year, ordered = TRUE),
          colors = "RdYlBu",
          type = "bar",
          orientation = "h",
          text = ~paste(round(vizCredPrcnt*100, 2), "%")) %>%
  layout(xaxis = list(title = "Percent Held"),
         yaxis = list(title = "Credentials"),
          annotations = list(x = credInput()$vizCredPrcnt,
                                  y = forcats::fct_reorder(credInput()$CredentialQ, credInput()$group_vizCredPrcntMean),
                                  text = paste(round(credInput()$vizCredPrcnt * 100, 2), "%"),
                                  xref = "x",
                                  x = 0,
                                  xanchor = "auto",
                                  yref = "y",
                                  y = 0,
                                  yanchor = "auto",
                                  showarrow = FALSE),
         #margin parameter to show all y label text
         margin = list(l = 160))

I’d greatly appreciate any insight, and many thanks!

Hey @ajmasnyj

Have you tried using direct labels textposition = 'auto' instead of annotations? For example,

x <- c('Product A', 'Product B', 'Product C')
y <- c(20, 14, 23)
y2 <- c(18, 13, 27)
text <- c('27% market share', '24% market share', '19% market share')
data <- data.frame(x, y, text)

p <- plot_ly(data, orientation = 'h') %>%
  add_trace(x = ~y, y = ~x, type = 'bar', 
             text = y, textposition = 'auto',
             marker = list(color = 'rgb(158,202,225)',
                           line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
  add_trace(x = ~y2, y = ~x, type = 'bar', 
            text = x, textposition = 'auto',
            marker = list(color = 'rgb(158,202,225)',
                          line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
  layout(title = "January 2013 Sales Report",
         xaxis = list(title = ""),
         yaxis = list(title = ""))

Thanks, @bcd , for your reply, and apologies for the delay. I like your approach, however, I’m struggling to apply this to my script, as my data is a reactive output that is in the long tidy format, and the use of multiple traces would mean ungrouping the data back to a wide format. Happy to hear your thoughts and any other suggestions!

I have the same question. With long data format, how to show direct labels on Stacked bar chart?Did you find the solution?