I want to draw a ggplot2 barchart with overlayed bars (percentages). I have 2 groups and each of the groups consists of 2 subgroups.
Consider the following reproducible example:
# Create data for ggplot
data_ggp <- data.frame(category = rep(c("cat1", "cat2", "cat3"), 4),
values = c(0.664, 0.045, 0.291, 0.482, 0.029, 0.489, 0.537, 0.027, 0.436, 0.536, 0.028, 0.436),
group = c(rep("group1a", 3), rep("group1b", 3), rep("group2a", 3), rep("group2b", 3)))
This is what I got so far:
library("ggplot2")
# Draw barchart (not overlayed)
ggplot(data_ggp, aes(category, values)) +
geom_bar(stat = "identity", aes(fill = group), position = "dodge")
In this example, the red and green bars should overlay each other and the blue and purple bars should overlay each other.
I am sure there must be an easy solution and I found the following 2 threads:
However, both of the threads describe slightly different problems and I was unfortunately not able to modify the codes for my specific situation.