Using the following data frame I aim to stack all x values by group using geom_bar or geom_col and fill them by subgroup.
dat <- data.frame(group = c(rep("H", 4), rep("T",4)), subgroup = rep(c("A", "B", "C", "D"), 2),
x = c(100, 10, 80, 50, 40, 50, 80, 70),
y = c(20, 5, 50, 20, 20, 10, 40, 50))
But I also want to fill each stacked x value with their corresponding y value. However when I use the following code I get the following bar plot.
dat %>%
ggplot(aes(x = group, y = x, fill = subgroup)) +
geom_col(alpha = .5) +
geom_col(aes(x = group, y = y, fill = subgroup), alpha = 1)
Can anyone help me to fill a fraction of each subgroup with the y values please? All I have been able to get is an overlapped stack of y values, but not mapped to each color.
I have tried grouping the values and adding a key. however I am stuck with the previous plot