rggplot2stacked-bar-chart

How to make a proportional stacked bar chart in with one bar for totals from a pivot longer dataframe


I would like to make a stacked bart chart showing percentages of the variables SARREN, SCED and SPROP across 4 case studies and have also a column for the totals across the 4 cases.

I modified the database with pivot longer to have one column with the three variables and managed to create a stacked bar chart with counts, but when including position = "fill" I get the message: Ignoring unknown aesthetics: position.

Any help would be much appreciated. I would also like to add labels to each stacked bar with percentages for each variable, but did not find a solution yet. I wold also be very thankful if anyone could give me a hint on this respect.

This is an extract of my ownership database:

    structure(list(SPROP = c(100L, 0L, 0L, 70L, 6L, 0L, 0L, 0L, 10L, 
45L, 100L), SARREN = c(0L, 100L, 100L, 20L, 94L, 100L, 100L, 
100L, 90L, 55L, 0L), SCED = c(0L, 0L, 0L, 10L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), CASE = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L)), row.names = 10:20, class = "data.frame")

And this is my code so far:

ownership2 <- ownership %>%
  pivot_longer(names_to = "variable", values_to = "value",-CASE)%>%
  group_by(variable) %>%
  
ownership3 <- ownership2 %>%
  mutate(case1 = as.factor(CASE))


# total count
ownership3 %>%
  ggplot(aes(x=case1))+
  geom_bar(aes(fill=factor(variable))) +
  geom_bar(aes(x="Total", fill=factor(variable)))

Solution

  • I think you're looking for something like this.
    Take a look:

    # Pkgs 
    library(tidyverse)
    library(palmerpenguins)
    
    # Toy data
    toy_data <- palmerpenguins::penguins %>% 
      mutate(species = factor(consecutive_id(species))) %>% 
      select(3:5, 1) %>% 
      filter(if_all(everything(), \(x) !is.na(x)))
    
    colnames(toy_data) <- c("sprop", "sarren", "sced", "case")
    
    # Wrangling to a longer format
    toy_data <- pivot_longer(toy_data, -case)
    
    # The plot
    ggplot() +
      geom_col(
        aes(x = case, y = value, fill = name),
        toy_data,
        position = "fill") +
      
      geom_col(
        aes(x = case, y = value, fill = name),
        summarise(toy_data, .by = name, case = "Total", value = sum(value)),
        position = "fill")
    

    Created on 2024-04-26 with reprex v2.1.0