使用ggplot在分组堆栈图表中的条之间的间距

时间:2022-06-30 14:57:53

My question is a continuation for the below question.

我的问题是以下问题的延续。

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

在ggplot中生成成对的堆积条形图(仅对某些变量使用position_dodge)

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 ?

在我的数据中,分组的长度(类型)可以变化。这使得我的条宽,位置不一致。我能够通过基于组(类型)的长度动态分配来修复我的宽度。但是,由于位置闪避不能与堆栈一起工作,因此我无法改变条形之间的间隙。有没有办法可以改变酒吧之间的差距?

1 个解决方案

#1


2  

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

你的意思是每个酒吧之间的距离相等吗?下面的代码添加了width参数:

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)

使用ggplot在分组堆栈图表中的条之间的间距

#1


2  

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

你的意思是每个酒吧之间的距离相等吗?下面的代码添加了width参数:

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)

使用ggplot在分组堆栈图表中的条之间的间距