Image credit: MalditoBarbudo (CC-BY 4.0)

# Starting bars and histograms at zero in ggplot2

When creating histograms or barplots in ggplot2 we found that the data is placed at some distance from the x axis, which means the y axis starts below zero:

This is because, internally, ggplot2 is expanding x and y axes by a multiplicative or additive constant1. This makes sense in almost all plots, except for the bar and histogram ones, as we see above.

## Workaround

To avoid this behaviour, we have to modify the y scale indicating the expand value as well as the limits value:

As you can see, here we add the following layer to the gpplot call:

scale_y_continuous(expand = c(0,0),
limits = c(0,30)) +


This way we avoid the y axis expand, but we need to set (harcode) the y max limit to allow for a space above the data (the y max value depends on the count range, so you have to take a look at the plot first).

• You have to provide a hardcoded limit value, so you have to generate the plot one time and modify the code to generate the final version. So no automatization here, in case you need it.

• It does not play well with free y faceted plots.

### Barplots

For barplots is the same:

## Summary

As you can see, default plot adds a gap at both ends, up and down. Setting expand to zero removes both gaps, no ideal. Finally setting expand to zero and setting the optimal y max limit does the trick.

## Bits of code

Code for the summary cowplot:

## Sources

1. See the expand parameter in ?scale_y_continuous for details

2. It seems an asymmetrical expand argument is on its way