(Originally I saw this issue on https://github.com/tidyverse/ggplot2/issues/2879#event-1825710387)
Suppose I want to plot a bar chart with x axis ranged from 2 to 4. It's not enough to set
limits of x axis as
[2, 4], since it doesn't cover the width of bars. So, I need to manually add 0.5, which is half the width of the bar, to the limits. This seems too tricky.
Are there any good way to ensure the whole bars are included in the limits? (Note that
expand seems to expand the limit after cutting the data, so it doesn't help here.)
library(ggplot2) d <- data.frame(x = 1:5) ggplot(d) + geom_bar(aes(x)) + xlim(2, 4) #> Warning: Removed 2 rows containing non-finite values (stat_count). #> Warning: Removed 2 rows containing missing values (geom_bar).
ggplot(d) + geom_bar(aes(x)) + xlim(1.5, 4.5) #> Warning: Removed 2 rows containing non-finite values (stat_count).
Created on 2018-09-05 by the reprex package (v0.2.0).