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.
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:
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).
This way has some disadventages2:
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.
For barplots is the same:
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.