简体   繁体   中英

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 ?

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM