ggplot2 - Stacked bar with different width

Viewed 1347

I want to produce a reverse pyramid graph where the bar stacked on each other with different width.

1st, I have a stacked bar chart as below code sample

library(dplyr)
library(ggplot2)
sample <- data_frame(x=c(1, 1, 1, 1, 2, 2, 2, 2),
                     y=c(5,10,15, 20, 10, 5, 20, 10),
                     w=c(1, 2, 3, 4, 1, 2, 3, 4),
                     group=c("a", "b", "c", "d", "a", "b", "c", "d"))

ggplot() +
    geom_bar(data=sample,
             aes(x=x,y=y,group=group, fill=group),
             stat="identity", position=position_stack())

enter image description here

Then I added the width to aes so the one with lower w value will be smaller while they still stacked on each other. However, the bars didn't stack with warnings.

ggplot() +
geom_bar(data=sample,
         aes(x=x,y=y,group=group, fill=group, width=w/5),
         stat="identity", position=position_stack())

enter image description here

Warning: Ignoring unknown aesthetics: width
Warning message:
position_stack requires non-overlapping x intervals 

Any helps on make bar plot stacked or ideas on different plot type that can cover similar concepts would be highly appreciated. Thanks!

2 Answers
Related