Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove space between bars in ggplot2 geom_bar

I am looking to "dodge" the bars of a barplot together. The following R code leaves white space between the bars. Other answers like this one show how to accomplish this for the bars part of a group, but that does not seem to apply for distinct bars per factor on the x axis.

require(ggplot2)
dat <- data.frame(a=c("A", "B", "C"), b=c(0.71, 0.94, 0.85), d=c(32, 99, 18))

ggplot(dat, aes(x= a, y = b, fill=d, width = d/sum(d))) +
  geom_bar(position=position_dodge(width = 0.1), stat="identity")

Playing with the width variable changes the appearance, but it does not seem possible to get the bars to sit side by side while still retaining their meaningful difference in width (in this graph redundantly represented by the fill colour too).

like image 366
Frederik Vanrenterghem Avatar asked Jan 31 '17 12:01

Frederik Vanrenterghem


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). You can set this argument to a lower value to get bars that are narrower with more space between them.

How do I reduce the space between bars in R?

R – Set Space between Bars in Bar Plot To set space between bars in Bar Plot drawn using barplot() function, pass the required spacing value for space parameter in the function call. space parameter is optional and can accept a single value or a vector to set different space between bars in the bar plot.

How do I make all the bars the same size in ggplot2?

We can make the bar widths in a grouped barplots the same size using the position argument to geom_col() function. We specify position_dodge2(preserve = “single”) to make the widths the same. And this option is available from ggplot2 version 3.3 and above.

How do I change the size of a bar in ggplot2?

To Increase or Decrease width of Bars of BarPlot, we simply assign one more width parameter to geom_bar() function.


2 Answers

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

enter image description here

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

like image 122
Mark Peterson Avatar answered Sep 28 '22 00:09

Mark Peterson


It is possible by using a continuous x axis and relabel it.

ggplot(dat, aes(x=cumsum(d/sum(d))) - d/sum(d)/2, y = b, fill=d, width=d/sum(d))) +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_x_continuous(breaks=cumsum(dat$d/sum(dat$d)) - dat$d/sum(dat$d)/2, labels=dat$a)

Or isn't this what you where looking for

like image 45
drmariod Avatar answered Sep 28 '22 00:09

drmariod