Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting distance between groups of bars in ggplot2

Tags:

r

ggplot2

This is my data:

> sum.ex  
        Timepoint     mean  n       sd Time   Group  
A1             A1-All 1.985249 26 1.000180   A1     All  
A1-pT2D       A1-pT2D 1.913109 13 1.012633   A1    pT2D  
A1-Control A1-Control 2.934105 13 2.472951   A1 Control  
B1             B1-All 2.555601 25 1.939970   B1     All  
B1-pT2D       B1-pT2D 2.057389 13 1.023416   B1    pT2D  
B1-Control B1-Control 2.145555 12 1.089522   B1 Control  

This is my code:

png('ex')  
ggplot(sum.ex, aes(x = Timepoint, y = mean)) +  
   geom_bar(width = 0.5, position = position_dodge(width = 200), stat="identity", aes(fill =     Group)) +  
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), size = 1, shape = 1, width = 0.2) +  
  scale_fill_manual(values = c("#333333", "#FF0000", "#0000FF")) +  
   xlab(NULL) +
  ggtitle("PLIN1") + theme_bw() + theme(panel.grid.major = element_blank())   
 dev.off()

This is the output:

enter image description here

However, I want to have Black+Red+Blue really close, then a space and then Black+Red+Blue really close again.

Thank you!

like image 757
user3236594 Avatar asked Feb 14 '14 07:02

user3236594


People also ask

How do I change the spacing between bars in ggplot2?

Set the width of geom_bar() to a small value to obtain narrower bars with more space between them. By default, the width of bars is 0.9 (90% of the resolution of the data). You can set this argument to a lower value to get bars that are narrower with more space between them.

How do I adjust the width of a bar in R?

To make the bars narrower or wider, set the width of each bar with the width argument. Larger values make the bars wider, and smaller values make the bars narrower. To add space between bars, specify the space argument. The default value is 0.2.

What is the difference between Geom_bar and Geom_col?

There are two types of bar charts: geom_bar() and geom_col() . geom_bar() makes the height of the bar proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use geom_col() instead.


2 Answers

I think this is easiest to achieve if you use x = Time and fill = Group. Something like:

dodge <- position_dodge(width = 0.5)
ggplot(df, aes(x = Time, y = mean, fill = Group)) +  
  geom_bar(width = 0.5, stat="identity", position = dodge) +  
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd),
                position = dodge, size = 1, shape = 1, width = 0.2) +  
  scale_fill_manual(values = c("#333333", "#FF0000", "#0000FF")) +
  theme_bw() +
  theme(panel.grid.major = element_blank())

enter image description here

like image 91
Henrik Avatar answered Sep 24 '22 10:09

Henrik


Plot against Time only, then position_dodge has meaning for the bars (there are 3 observations per group). Use position_dodge with width close to the bar width. Add group=Group to make the errorbars behave like the bars (you need it since they don't have colour aesthetic to distinguish them). Use the same position_dodge width as before to align them properly.

ggplot(sum.ex, aes(x = Time, y = mean)) +  
  geom_bar(width = 0.5, position = position_dodge(width = 0.5), stat = "identity", aes(fill = Group)) +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd, group=Group), position=position_dodge(width = 0.5), size = 1, shape = 1, width = 0.2) +  
  scale_fill_manual(values = c("#333333", "#FF0000", "#0000FF")) +  
  xlab(NULL) +
  ggtitle("PLIN1") + theme_bw() + theme(panel.grid.major = element_blank())  
like image 29
Rosen Matev Avatar answered Sep 22 '22 10:09

Rosen Matev