Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove space between bars ggplot2

Tags:

r

ggplot2

This is my code

    ggplot(df, aes(x=timepoint, y=mean, fill=group)) +
            geom_bar(position=position_dodge(.3), colour="black", stat="identity", width=0.3, , binwidth=0) +
            geom_errorbar(position=position_dodge(.3), width=.25, aes(ymin=mean, ymax=mean+sem)) +
            scale_fill_manual(values=c("#FFFFFF", "#000000"), guide=FALSE) +
            theme_bw() +
            ylab(ylab) +
            xlab("") +
            # xlim("Baseline", "12w") +
            scale_x_discrete(expand = c(0,0), limits=c("Baseline","12w")) + 
            scale_y_continuous(expand = c(0,0) ) + 
            theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
                  panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))

This is MY output, which I DONT want, its too much space between "baseline" and "12w":

enter image description here

How to remove the space between the bars?

Thank you

df is like this:

df <- structure(list(group = c("a1.d.ffa.mean Dysglyc", "a1.c.ffa.mean Control", 
"b1.d.ffa.mean Dysglyc", "b1.c.ffa.mean Control"), timepoint = c("Baseline", 
"Baseline", "12w", "12w"), mean = c(1.913509, 2.181959, 2.742249, 
1.50846), sem = c(0.10663114, 0.08360294, 0.07890374, 0.08348542
), p.value = c(0.597738161, 1, 0.007885464, 1), p.value.t = c(0.04408, 
1, 0.2455049, 1)), .Names = c("group", "timepoint", "mean", "sem", 
"p.value", "p.value.t"), class = "data.frame", row.names = c(NA, 
-4L))

df

                  group timepoint     mean        sem     p.value p.value.t
1 a1.d.ffa.mean Dysglyc  Baseline 1.913509 0.10663114 0.597738161 0.0440800
2 a1.c.ffa.mean Control  Baseline 2.181959 0.08360294 1.000000000 1.0000000
3 b1.d.ffa.mean Dysglyc       12w 2.742249 0.07890374 0.007885464 0.2455049
4 b1.c.ffa.mean Control       12w 1.508460 0.08348542 1.000000000 1.0000000
like image 374
nOOb Avatar asked Sep 17 '14 09:09

nOOb


People also ask

How do I reduce the gap 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).

How do I make bars thinner 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.

How do I increase the space between grouped bars in ggplot2?

For grouped bars, there is no space between bars within each group by default. However, you can add some space between bars within a group, by making the width smaller and setting the value for position_dodge to be larger than width.


1 Answers

Just adjust the widths:

ggplot(df, aes(x=timepoint, y=mean, fill=group)) +
  geom_bar(position=position_dodge(0.9), colour="black", stat="identity", width=0.9, , binwidth=0) +
  geom_errorbar(position=position_dodge(0.9), width=0.85, aes(ymin=mean, ymax=mean+sem)) +
  theme_bw() 

enter image description here

like image 85
Roland Avatar answered Oct 08 '22 13:10

Roland