0

I've looked at lots of questions on this but nothing seems to get it quite right.

I have this bar chart of soccer matches that's stacked to show which team had better or worse form and how that mapped to the result.

I want to add a label to each of the segments of the bars to show the total for that segment -- eg, 15 for "Blades and draw" -- and also a total for the bar at the top, ideally.

Here's the chart: enter image description here

Here's the code used to create it:

ggplot(Sheff_derby_form_league, aes(x = Result)) + 
geom_bar(aes(y = ..count.., fill = Res_vs_f_team)) +
labs(title = "Blades vs Owls. League derby form and result",
   subtitle = "Team with form advantage a little more likely to win", 
   x = "Better form: > 0.33ppg over opponent. Even form: < 0.33ppg", y = "") 
   + scale_fill_brewer(palette = "RdBu")
Mr_Percy_Heat
  • 59
  • 1
  • 10

1 Answers1

0

Since you haven't provided data, I am using one of the default datasets. You can easily make labeled stacked bar chart using ggbarstats function from ggstatsplot (https://indrajeetpatil.github.io/ggstatsplot/reference/ggbarstats.html) that additionally gives some other helpful details. As the multiple examples show below, the defaults can be modified to your liking.

# setup
set.seed(123)
library(ggplot2)
library(ggstatsplot)

# percentage
ggstatsplot::ggbarstats(
  data = mtcars,
  main = am,
  condition = cyl,
  messages = FALSE
)

# count
ggstatsplot::ggbarstats(
  data = mtcars,
  main = am,
  condition = cyl,
  data.label = "count",
  package = "wesanderson",
  palette = "Royal2",
  messages = FALSE
)

# count and percentage
ggstatsplot::ggbarstats(
  data = mtcars,
  main = am,
  condition = cyl,
  data.label = "both",
  messages = FALSE
) +
  ggplot2::labs(subtitle = NULL)

Created on 2019-02-13 by the reprex package (v0.2.1)

Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51