title: How to Make R Plotly Grouped Bar Plot (with Data Labels)?
body: <p>I'm trying to create a grouped bar plot with plotly in R. I can create what I want with ggplot but I can't use ggplotly because of the issue where it does not display negative numbers correctly (<a href="https://github.com/ropensci/plotly/issues/560" rel="nofollow noreferrer">https://github.com/ropensci/plotly/issues/560</a>). I have two questions that I am hoping you can help with:</p>
  
  <ol>
  <li>How do I make it so that the bars that are in the same group are side by side? For example, in the plot below the Age bars (Young, Middle, and Old) are not touching even though those bars are all part of the Age group.</li>
  <li>Also, how can I have each annotation horizontally centered over the bar it is associated with? For example, the age labels are currently centered over the x axis label Age rather than over their respective bars. If this is not possible having the annotation underneath the x axis in some way would also work.</li>
  </ol>
  
  <p>Here's my code so far:</p>
  
  <pre><code># Create example data
  example_data &lt;- as.data.frame(matrix(NA, 9, 3))
  names(example_data) &lt;- c("group", "subgroup", "value")
  example_data$group &lt;- c("Gender", "Gender", "Race", "Race", "Race", "Age", "Age", "Age", "Human")
  example_data$subgroup &lt;- c("Female", "Male", "Asian", "Black", "White", "Young", "Middle", "Old", "Yes")
  example_data$value &lt;- c(11, 14, 72, 12, 82, 22, 30, 4, 5)
  
  # Create chart
  p &lt;- plot_ly(
    data = example_data,
    x = ~group,
    y = ~value,
    color = ~subgroup,
    type = "bar"
  ) %&gt;%
    layout(
      # xaxis = list(type="category"), # Doesn't seem to do anything
      # barmode = "group", # Doesn't seem to do anything
      showlegend = FALSE
  ) %&gt;%
  add_annotations(text = ~subgroup,
                  x = ~group,
                  y = ~value / 2, # Center vertically on the bar
                  # xref = "x", # Doesn't seem to do anything
                  # yref = "y", # Doesn't seem to do anything
                  # xanchor = 'center', # Doesn't seem to do anything
                  # yanchor = 'bottom', # Doesn't seem to do anything
                  showarrow = FALSE)
  
  p
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/eMRrX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eMRrX.jpg" alt="plotly grouped bar plot example"></a></p>