Search code examples
rggplot2facetgeom-barstacked-chart

Add stacked bar graphs inside faceted graphs


I'm plotting 3 columns/character vectors in a faceted bar graph and would like to be able to plot "smoker" as the stacked bar graph inside each bar graph.

Sample Data

I'm using ggplot2. I've managed to plot "edu" and "sex" already, but I'd also like to be able to see the count of each "y" and "n" inside each bar graph of "sex" (divided along the x-axis by "edu"). I have attached an image of my graph,

Resulting Plot

which I achieved by entering the following code:

I tried entering the "fill=smoker" argument in aes, but this didn't work. If anyone has any suggestions on how to clean up the code I used to turn the graph into a faceted one and express it as percentages, I would also be very grateful, as I took it from somewhere else.

test <- read.csv('test.csv', header = TRUE)
library(ggplot2)
ggplot(test, aes(x= edu, group=sex)) + 
    geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count", show.legend = FALSE) +
    geom_text(aes( label = scales::percent(..prop..),
                   y= ..prop.. ), stat= "count", vjust = -.5, size = 3) +
    labs(y = NULL, x="education") +
    facet_grid(~sex) +
    scale_y_continuous(labels = scales::percent)

Solution

  • Not sure if this is what you are looking for but I attempted my best at answering your question.

    library(tidyverse)
    library(lubridate)
    library(scales)
    
    
    test <- tibble(
     edu = c(rep("hs", 5), rep("bsc", 3), rep("msc", 3)),
     sex = c(rep("m", 3), rep("f", 4), rep("m", 4)),
     smoker = c("y", "n", "n", "y", "y", rep("n", 3), "y", "n", "n"))
    
    
    test %>%
     count(sex, edu, smoker) %>%
     group_by(sex) %>%
     mutate(percentage = n/sum(n)) %>%
     ggplot(aes(edu, percentage, fill = smoker)) +
     geom_col() +
     geom_text(aes(label = percent(percentage)),
       position = position_stack(vjust = 0.5)) +
     facet_wrap(~sex) +
     scale_y_continuous(labels = scales::percent) +
     scale_fill_manual(values = c("#A0CBE8", "#F28E2B"))
    

    enter image description here