2

I want to create a waterfallchart with several groups where all the groups start at 0.

This is my code:

gdp <- data.frame("Country"=rep(c("China", "USA"), each=2),
                  "Type"=rep(c("GDP2013", "GDP2014"), 2),
                  "Cnt"= c(16220, 3560, 34030, -10570))

gdp <- gdp %>%
  mutate(start=Cnt,
         start=lag(start),
         end=ifelse(Type=="GDP2013", Cnt, start+Cnt),
         start=ifelse(Type=="GDP2013", 0, start),
         amount=end-start,
         id=rep(1:2, each=2))


gdp %>%
  ggplot(aes(fill=Type)) +
  geom_rect(stat="identity", aes(x=Country, 
                xmin=id-0.25, 
                xmax=id+0.25, 
                ymin=start, 
                ymax=end))

The two bar types should be ordered next to each other per group and USA GDP2014 should start at the height of USA GDP2013 but end 10570 lower.

I know that I could do this with a facet_wrap but I want no separation between groups (e.g. facets.

BeSeLuFri
  • 623
  • 1
  • 5
  • 21

1 Answers1

1

geom_rect takes a position parameter.

I believe position='dodge' does what you require if I understand your question correctly.

enter image description here

More info: https://ggplot2.tidyverse.org/reference/position_dodge.html

hdkrgr
  • 1,666
  • 1
  • 12
  • 22