title: Create a grouped bar chart with the groups being all the unique values of a dataframe column
body: <p>I have the dataframe below and I want it to be displayed as a grouped bar chart as follows. In the <code>x axis</code> there should be the <code>year</code> while in the <code>y axis</code> count n. The complex thing is that I want the <code>severity</code> to be displayed as a grouped bar chart with a different color for every unique value of <code>severity</code> <code>(2,3,4)</code>.
  something like:</p>
  <p><a href="https://i.stack.imgur.com/nrPYE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nrPYE.jpg" alt="enter image description here" /></a></p>
  <pre><code>library(plotly)
  df2&lt;-structure(list(year = c(&quot;2017&quot;, &quot;2018&quot;, &quot;2018&quot;, &quot;2019&quot;, &quot;2019&quot;, 
  &quot;2019&quot;, &quot;2019&quot;, &quot;2019&quot;, &quot;2020&quot;, &quot;2020&quot;, &quot;2020&quot;, &quot;2020&quot;, &quot;2020&quot;
  ), severity = c(&quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;2&quot;, &quot;2&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;2&quot;, 
  &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;4&quot;), accident.description = c(&quot;right lane blocked&quot;, 
  &quot;two lanes blocked&quot;, &quot;road closed&quot;, &quot;one lane blocked&quot;, &quot;right lane blocked&quot;, 
  &quot;one lane blocked&quot;, &quot;right lane blocked&quot;, &quot;two lanes blocked&quot;, 
  &quot;right lane blocked&quot;, &quot;right and center lane blocked&quot;, &quot;right lane blocked&quot;, 
  &quot;two lanes blocked&quot;, &quot;two lanes blocked&quot;), n = c(3L, 1L, 1L, 
  1L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 2L)), class = c(&quot;grouped_df&quot;, 
  &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -13L), groups = structure(list(
      year = c(&quot;2017&quot;, &quot;2018&quot;, &quot;2018&quot;, &quot;2019&quot;, &quot;2019&quot;, &quot;2020&quot;, 
      &quot;2020&quot;, &quot;2020&quot;), severity = c(&quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;2&quot;, &quot;3&quot;, &quot;2&quot;, 
      &quot;3&quot;, &quot;4&quot;), .rows = structure(list(1L, 2L, 3L, 4:5, 6:8, 9L, 
          10:12, 13L), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;, 
      &quot;vctrs_vctr&quot;, &quot;list&quot;))), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;
  ), row.names = c(NA, -8L), .drop = TRUE))
  
  fig &lt;- plot_ly(df2, x = ~year, y = ~n, type = 'bar', name = 'severity',
                 hovertext = paste(
                   &quot;&lt;br&gt;Year :&quot;,
                   df2$year,
                   &quot;&lt;br&gt;Severity :&quot;,
                   df2$severity,
                   &quot;&lt;br&gt;Accident Description :&quot;,
                   df2$accident.description,
                   paste(&quot;&lt;br&gt; Count:&quot;),
                   df2$n
                 ),
                 hoverinfo = &quot;text&quot;)
  fig
  </code></pre>