Home > Back-end >  Stacked percentage barplot using facet wrap to group
Stacked percentage barplot using facet wrap to group

Time:12-23

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.

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

CodePudding user response:

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()

CodePudding user response:

Here is a slightly different approach as of dear @stefan. Here we use x="":

library(tidyverse)

TestData %>% 
  ggplot(aes(x = "", y= Percent, fill=factor(Stroke), label = scales::percent(Percent)))  
  geom_col(position = position_stack())  
  facet_wrap(~ Ethnicity, ncol = 3, strip.position = "bottom")  
  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()

enter image description here

  • Related