Remove Space Between Bars Ggplot2

Remove space between bars ggplot2

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()

Sample Image

ggplot2 : How to reduce the width AND the space between bars with geom_bar

I would adjust the plot's aspect ratio, and have ggplot automatically assign the right width for the bars and the gap between them:

  ggplot(iris, aes(Species, Petal.Length)) + 
geom_bar(stat="summary", width=0.4) +
theme(aspect.ratio = 2/1)

Produces this:

Sample Image

Reduce space between groups of bars in ggplot2

Forgive me if I completely missed what your trying to accomplish here but the only reason that ggplot has included so much white space is because you constrained the bars to a particular width and increased the size of the graph.
The white space within the graph is an output of width of the bars and width of the graph.

Using your original graph...
orig graph

We notice a lot of whitespace but you both made the bins small and your graph wide. Think of the space as a compromise between bins and whitespace. Its illogical to expect a wide graph with small bins and no whitespace. To fix this we can either decrease the graph size or increase the bin size.

First we increase the bin size back to normal by removing your constraints.
Which looks rediculous....

Sample Image

But by looking at the Remove space between bars ggplot2 link that you included above all he did was remove constraints and limit width. Doing so would result in a similar graph...

similar graph

Including the graph from your link above....
Sample Image

And removing all of your constraints....
Sample Image

    example<-ggplot(mydf, aes(factor(Response), Count, fill = Treatment)) + 
geom_bar(stat="identity",position = position_dodge()) +
scale_fill_manual(values = aPalette, name = "Treatment") +
theme_bw() +
labs(x = "Response", y = "Count")

example

If your goal was not to make your graph similar to the one in the link by removing whitespace let me know, other then that I hope this helped.

Remove space between bars in ggplot2 geom_bar

I would generate my x-positions and widths first, then pass them in to the aesthetics and override to make your factor labels:

First, store the width

dat$width <-
dat$d / sum(dat$d)

Then, assuming that your data.frame is in the order you want it plotted, you can set the location as the cumulative sum of the widths. Note, however, that that cumulative sum is where you want the right edge of the bar to be, so to get the center you need to subtract half of the width:

dat$loc <-
cumsum(dat$width) - dat$width/2

Then, pass it all in to the ggplot call, setting your labels explictly:

ggplot(dat, aes(x= loc, y = b, fill=d, width = width)) +
geom_bar(stat="identity") +
scale_x_continuous(breaks = dat$loc
, labels = dat$a)

gives

Sample Image

I am not sure about the advisability of this appproach, but this should get the job done.

Different width of bars and remove white space in geom_bar()

Thanks for the suggestions everyone. I managed to find a workaround using facet_grid(). It is not the most elegant solution, but it works nonetheless.

require(ggplot2)

df <- data.frame(order=c(1,2,3),
rate=c(28.6, 30.75, 25.25),
lower=c(24.5, 28.94, 22.86),
upper=c(31.26, 33.1, 28.95),
width=c(.25,1.25,.5))

plot <- ggplot(data=df, aes(x=order, y=rate, width=width)) +
geom_bar(aes(fill=as.factor(order)), stat="identity") +
geom_errorbar(aes(x=order, ymin=lower, ymax=upper), width=.1) +
facet_grid(~as.factor(order), scales='free_x', space='free', switch="x") +
scale_y_continuous(expand=c(0, 0)) +
scale_x_continuous(expand=c(0, 0)) +
theme_bw() +
theme(panel.border=element_blank(),
panel.spacing.x=unit(0, "lines"),
strip.background=element_blank(),
strip.placement="outside",
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position="none")

The main issue was in getting the correct alignment with geom_errorbar() and geom_bar(). The facet_grid option maintains the alignment. Adding the panel.spacing.x=unit(0,"lines") removes the white space between the bars by changing the space between each panel. The key is that width must be at least 1. This solution does cause problems with the alignment of the x-axis tick marks, but for my purposes this was not an issue.



Related Topics



Leave a reply



Submit