Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I understand that one can change the width of a bar in geom_bar using the width argument. That does work, but then it creates a larger gap between the bars. Is there a way to manually push the bars closer to together? Should I be manipulating the axis somehow instead?

Here is an example, changing width to 0.3 on the right to get the desired bar width.

library(tidyverse)
library(gridExtra)

p1 <- ggplot(iris, aes(Species, Petal.Length)) + geom_bar(stat="summary")
p2 <- ggplot(iris, aes(Species, Petal.Length)) + geom_bar(stat="summary", width=0.3)
grid.arrange(p1,p2,nrow=1)

enter image description here

Note: I know this question is similar to this one, but the answer to closing the gap was not apparent.

How to change the space between bars in geom_bar?

like image 214
Alex Avatar asked Apr 28 '18 14:04

Alex


People also ask

How do I change the width 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.

How do I reduce 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_col and Geom_bar?

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.


2 Answers

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:

enter image description here

like image 85
onlyphantom Avatar answered Oct 19 '22 09:10

onlyphantom


Rather than taking the width smaller, which narrows the bars but increases the inter-bar space, set width = 1 to remove all space between.*

ggplot(iris, aes(Species, Petal.Length, fill=Species)) + geom_bar(stat="summary", width=1)

enter image description here

The default value is 0.9, so you can get very small spaces by setting width = 0.95

ggplot(iris, aes(Species, Petal.Length, fill=Species)) + geom_bar(stat="summary", width=0.95)

enter image description here

  • With fill=Species, I took the liberty of adding color to help see the different bars when there is no space between.
like image 27
Dannid Avatar answered Oct 19 '22 08:10

Dannid