vishnu
vishnu

Reputation: 11

spacing between bars in a grouped stack chart using ggplot

My question is a continuation for the below question.

Generate paired stacked bar charts in ggplot (using position_dodge only on some variables)

In my data the length of grouping(type) can vary. This makes my bar width, positions inconsistent. I was able to fix my width by dynamically assigning based on the length of group(type). However, the gap between bars is something which I was not able to change as position dodge doesn't work with the stack. Is there a way by which I can change the gaps between the bars ?

Upvotes: 0

Views: 3627

Answers (1)

lawyeR
lawyeR

Reputation: 7664

Do you mean equal distances between each bar? The code below adds the width argument:

df <- expand.grid(name = c("oak","birch","cedar"), sample = c("one","two"),
                  type = c("sapling","adult","dead")) 
df$count <- sample(5:200, size = nrow(df), replace = T) 

ggplot(df,aes(x=sample,y=count,fill=type))+ 
  geom_bar(stat = "identity", color="white", width = 0.6)+ facet_wrap(~name,nrow=1)

enter image description here

Upvotes: 2

Related Questions