Search code examples
rcsvggplot2graph

Creating stacked bar charts from CSV file in R with multiple categories on x-axis


I have a CSV file with something like that:

"partyname", "category1", "category2"
"FirstName", "2.0", "8.0"
"SecondName", "3.0", "9.0" 
"ThirdName", "4.0", "20.0"

With many more data point.

I want to create a stacked bar chart with partyname being the fill color in the chart, on the y-axis I want a percentage scale, and on the x-axis (this is where I struggle), I want to have all the categories (category1, etc.) being different labels (like if they were years going from 2010 to 2020).

All I have been able to do now is summing the categories together and creating on the x-axis that one category “summed up” which is not what I want.


Solution

  • You may get the data in long format for all the category values. Calculate the percentage value for each category and then plot a stacked bar chart.

    library(tidyverse)
    
    dat %>%
      pivot_longer(cols = starts_with('category'), names_to = "category") %>%
      mutate(value = round(prop.table(value) * 100, 2), .by = category) %>%
      ggplot(aes(category, value, fill = partyname)) + 
      geom_col() + 
      geom_text(aes(label = paste(value, '%')),position = position_stack(vjust = .5))
    

    enter image description here