0

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 ?

Community
  • 1
  • 1
vishnu
  • 11
  • 2
  • 2
  • Post your data and code. – zx8754 Jul 30 '15 at 13:16
  • data: `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)` and ggplot command is : `ggplot(df,aes(x=sample,y=count,fill=type))+ geom_bar(stat = "identity",color="white")+ facet_wrap(~name,nrow=1)` – vishnu Jul 30 '15 at 16:15

1 Answers1

2

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

lawyeR
  • 7,488
  • 5
  • 33
  • 63
  • Hi lawyeR, Thanks for the reply. I assume the width only changes the width of each bar not the gap between bars. My question is how do we change the gap between the bars. For suppose in the above data, there are only two types of names i.e. oak and birch the gap between the bars is too large. I am trying to reduce it so that the plot doesn't look too bad. – vishnu Jul 31 '15 at 05:41
  • Help us understand what you want. In the left panel of the plot in my example, you don't want the gap between "one" and "two" to be that size? Where is the "gap" you want to adjust in the plot above? Are you referring to the white horizontal lines between segments? Are you referring to the white vertical space between panels? – lawyeR Jul 31 '15 at 11:33
  • I am referring to the gap between one and two. – vishnu Aug 01 '15 at 12:17
  • I must be thick, not understanding you, but why can't you use the width = argument to widen or narrow the gap between one and two, as I did by narrowing the widths (and thus widening the gap)? – lawyeR Aug 01 '15 at 15:19