2

I have a bar plot with positive values on both sides. When I changed the width of the bars, the space between them became to large and doesn't look good. I tried to manipulate this with position = position_dodge, but it doesn't work. How can I reduce the spaces between the bars?

Here is the code (originally posted here Stacked barplot crossing the x-axis) with my data:

Year <- factor(c("2003-2009","2003-2009","2003-2009","2003-2009","2003-2009","2009-2012",
              "2009-2012","2009-2012","2009-2012","2009-2012"))
Q <- c(.05,.25,.5,.75,.95)
Score <- c(6,6,4,3,1,23,20,19,24,32)
df <- data.frame(Year, Q, Score)

df <- transform(df, Score=ifelse(as.character(Year) %in% c("2003-2009"), -Score, Score))
df.split <- split(df, df$Score < 0)

ggplot() + 
  geom_bar(data=df.split[[1]],aes(x=Q, y=Score, fill=Year), stat="identity",width = 0.09)+
  geom_bar(data=df.split[[2]],aes(x=Q, y=Score, fill=Year), stat="identity",width = 0.09)+
  geom_hline(yintercept=0) +
  coord_flip()+
  scale_y_continuous(labels=abs,limits=c(-40,40))+ 
  theme_bw()+
  scale_x_continuous(breaks=c(.05,.25,.5,.75,.95))
Community
  • 1
  • 1
Alicja
  • 33
  • 1
  • 3

1 Answers1

2

Treating the Q variable as a factor will set the space between the bars equal. Normally, when you reduce the width of the bars, the space between the bars increases. However, you want narrow bars and small spaces between the bars. You can achieve this by changing the height of the saved image.

The code (I also changed the width of the bars & the scale of the y-axis a bit):

ggplot() + 
  geom_bar(data=df.split[[1]],aes(x=as.factor(Q), y=Score, fill=Year), stat="identity", width = 0.4) +
  geom_bar(data=df.split[[2]],aes(x=as.factor(Q), y=Score, fill=Year), stat="identity", width = 0.4) +
  geom_hline(yintercept=0) +
  coord_flip() +
  scale_y_continuous(labels=abs,limits=c(-10,35)) + 
  theme_bw() +
  ggsave("myplot.png", width=8, height=2, dpi=300)

The result:

enter image description here

Note that I also deleted the scale_x_continuous(breaks=c(.05,.25,.5,.75,.95)) part of the plotting code as this will give an error when treating Q as a factor variable.

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Thank you for suggestions, but I've been wondering how to decrease spaces without changing the width of the bars. – Alicja Mar 31 '14 at 12:32
  • If I understand you correctly, you want narrow bars and small spaces between the bars. I updated my answer. Does this give you the desired result? – Jaap Mar 31 '14 at 13:17
  • Thank you. I've also experimented with "scale_x_discrete (expand = C (0,1.5))" at the end of the code. It also reduce spaces between the bars, but left too much below the lowest and above the highest bar. – Alicja Mar 31 '14 at 19:14
  • That's why I choose to set the dimensions of the plot in the `ggsave` code. If the answer worked for you, it would be appreciated if you accept the answer (and/or give the answer an upvote). This will give future readers a clue about the value of the solution. See also this help page: [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) – Jaap Apr 01 '14 at 06:44