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

Stacked percentage barplot using facet wrap to group

I have made a plot of my data but want the bars stacked as a percentage (the data already sums to 100%).

TestData <- tibble(Ethnicity = c(rep("Black",2), rep("Asian",2), rep("White",2)),
                   Stroke = c(0,1,0,1,0,1),
                   Percent = c(0.33, 0.67, 0.50, 0.50, 0.20, 0.80))

So, I want only ONE bar for the variable Stroke

        ggplot(TestData, aes(x=as.factor(Stroke),
                            y=Percent,
                            label = scales::percent(Percent),
                            fill = as.factor(Stroke) ) ) +    
            geom_bar(stat="identity", position="stack") + 
            facet_wrap(~ Ethnicity, ncol = 3, strip.position = "bottom")+
            scale_y_continuous(labels = scales::percent)+
            scale_fill_manual(values = c("#1380A1", "#FAAB18")) +
            geom_text(position = position_stack(vjust=0.5), colour="white", size = 3) + 
            theme_bw() +
            theme(legend.position="none") +         
            theme(axis.text.x = element_text(size=8)) +
            xlab("") +                              
            ylab("") +                                  
            ggtitle("Stroke among different ethnicities") +
            theme(plot.title = element_text(hjust = 0.5, size=10, face="bold")) +   
            coord_flip()

I have also tried melt to change the data to long format, but I still cannot get the plot to look correct. Where am I going wrong? Note: I have reviewed other answers, none deal with the facetting that I need.

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

library(reshape2)
TestDataMelted <- melt(TestData, id=c("Stroke", "Ethnicity"))

>Solution :

The issue is that you map Stroke on x. As you want only one stacked bar map a constant on x, e.g. factor(1) and use theme options to get rid of the y axis text and ticks:

library(ggplot2)

ggplot(TestData, aes(
  x = factor(1),
  y = Percent,
  label = scales::percent(Percent),
  fill = as.factor(Stroke)
)) +
  geom_col(position = "stack") +
  facet_wrap(~Ethnicity, ncol = 3, strip.position = "bottom") +
  scale_y_continuous(labels = scales::percent) +
  scale_fill_manual(values = c("#1380A1", "#FAAB18")) +
  geom_text(position = position_stack(vjust = 0.5), colour = "white", size = 3) +
  theme_bw() +
  theme(legend.position = "none") +
  theme(axis.text.x = element_text(size = 8), 
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank()) +
  labs(x = NULL, y = NULL, title = "Stroke among different ethnicities") +
  theme(plot.title = element_text(hjust = 0.5, size = 10, face = "bold")) +
  coord_flip()

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