Home > Software engineering >  stacked bar plot with fill of two count variables
stacked bar plot with fill of two count variables

Time:10-21

I am trying to create a stacked clustered bar graph with ggplot with Month on x-axis two count variables (HAPI and Admissions) as fill.

Code to reproduce sample data:

df <- data.frame(
  Group = c("Pre", "Pre", "Pre", "Pre", "Pre", "Pre", "Post", "Post", "Post", "Post", 
            "Pre", "Pre", "Pre", "Pre", "Pre", "Pre", "Post", "Post", "Post", "Post"),
  Month = c("Jan", "Feb", "March", "Apr", "May", "June", "June", "July", "Aug", "Sept", 
            "Jan", "Feb", "March", "Apr", "May", "June", "June", "July", "Aug", "Sept"),
  Unit = c(100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200),
  HAPI = c(0, 1, 0, 6, 1, 2, 0, 0, 0, 1, 0, 0, 6, 0, 3, 0, 0, 0, 0, 0),
  Admissions = c(83, 77, 79, 114, 101, 20, 62, 96, 92, 64, 84, 74, 92, 130, 90, 15, 44, 82, 79, 83)
)

I want it to look something like this if possible. enter image description here

  • Related