Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is ggplot only filling in some of my stacked bars and not others?

I’m making a stacked barplot using ggplot, but for some reason, it keeps leaving 2 bars unfilled, despite filling in other ones using the same criteria. Why is it doing this and how can I prevent this from happening?

library(ggplot2)
library(dplyr)
library(scales)
#Code to replicate

data <- tibble(team = factor(c(rep("Team 1", 10), rep("Team 2", 10), rep("Team 3", 10), rep("Team 4", 10)), levels = c("Team 1", "Team 2", "Team 3", "Team 4")),
               state = factor(c(rep(c("Won", "Tied",
                                      "Rematch", "Postponed", "Forfeit",
                                      "Lost", "Withdrew", "Ongoing",
                                      "Undetermined", "Unknown"), 4)), levels = c("Won", "Tied",
                                                                                  "Rematch", "Postponed", "Forfeit",
                                                                                  "Lost", "Withdrew", "Ongoing",
                                                                                  "Undetermined", "Unknown")),
               count = c(1920, 80, 241, 5, 310, 99, 2, 127, 20, 33,
                         48, 1, 8, 0, 11, 3, 0, 4, 3, 3,
                         140, 5, 8, 0, 17, 2, 0, 5, 3, 7,
                         477, 20, 59, 1, 106, 1, 0, 33, 7, 10))


data <- data %>%
  group_by(team) %>%
  mutate(percentage = round((count/sum(count, na.rm = TRUE)), 2))


data %>%
  ggplot(aes(fill= state, y = percentage, x = team)) + 
  geom_col(position="stack",width = 0.4) +
  coord_flip() +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0, 1)) +
  geom_text(aes(label = scales::percent(percentage, accuracy = 1)), 
            position = position_stack(vjust = .5), 
            check_overlap = TRUE )

Here’s how it looks; the floating 75% and 59% for Team 3 and Team 2, respectively, should be in the salmon color that is used for Teams 4 and 1. I know it’s not a typo because I’m using the same title for each.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Change the position argument to fill

data %>%
  ggplot(aes(fill= state, y = percentage, x = team)) + 
  geom_col(position="fill",width = 0.4) +
  coord_flip() +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0, 1)) +
  geom_text(aes(label = scales::percent(percentage, accuracy = 1)), 
            position = position_stack(vjust = .5), 
            check_overlap = TRUE )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading