4040# ' automatically determines the orientation from the aesthetic mapping. In the
4141# ' rare event that this fails it can be given explicitly by setting `orientation`
4242# ' to either `"x"` or `"y"`. See the *Orientation* section for more detail.
43- # ' @param width Bar width. By default, set to 90% of the resolution of the data.
43+ # ' @param just Adjustment for column placement. Set to `0.5` by default, meaning
44+ # ' that columns will be centered about axis breaks. Set to `0` or `1` to place
45+ # ' columns to the left/right of axis breaks. Note that this argument may have
46+ # ' unintended behaviour when used with alternative positions, e.g.
47+ # ' `position_dodge()`.
48+ # ' @param width Bar width. By default, set to 90% of the [resolution()] of the
49+ # ' data.
4450# ' @param geom,stat Override the default connection between `geom_bar()` and
4551# ' `stat_count()`.
4652# ' @examples
8086# ' ggplot(df, aes(x)) + geom_bar()
8187# ' # cf. a histogram of the same data
8288# ' ggplot(df, aes(x)) + geom_histogram(binwidth = 0.5)
89+ # '
90+ # ' # Use `just` to control how columns are aligned with axis breaks:
91+ # ' df <- data.frame(x = as.Date(c("2020-01-01", "2020-02-01")), y = 1:2)
92+ # ' # Columns centered on the first day of the month
93+ # ' ggplot(df, aes(x, y)) + geom_col(just = 0.5)
94+ # ' # Columns begin on the first day of the month
95+ # ' ggplot(df, aes(x, y)) + geom_col(just = 1)
8396geom_bar <- function (mapping = NULL , data = NULL ,
8497 stat = " count" , position = " stack" ,
8598 ... ,
99+ just = 0.5 ,
86100 width = NULL ,
87101 na.rm = FALSE ,
88102 orientation = NA ,
@@ -97,6 +111,7 @@ geom_bar <- function(mapping = NULL, data = NULL,
97111 show.legend = show.legend ,
98112 inherit.aes = inherit.aes ,
99113 params = list2(
114+ just = just ,
100115 width = width ,
101116 na.rm = na.rm ,
102117 orientation = orientation ,
@@ -123,16 +138,18 @@ GeomBar <- ggproto("GeomBar", GeomRect,
123138 params
124139 },
125140
126- extra_params = c(" na.rm" , " orientation" ),
141+ extra_params = c(" just " , " na.rm" , " orientation" ),
127142
128143 setup_data = function (data , params ) {
129144 data $ flipped_aes <- params $ flipped_aes
130145 data <- flip_data(data , params $ flipped_aes )
131146 data $ width <- data $ width %|| %
132147 params $ width %|| % (resolution(data $ x , FALSE ) * 0.9 )
148+ data $ just <- params $ just %|| % 0.5
133149 data <- transform(data ,
134150 ymin = pmin(y , 0 ), ymax = pmax(y , 0 ),
135- xmin = x - width / 2 , xmax = x + width / 2 , width = NULL
151+ xmin = x - width * (1 - just ), xmax = x + width * just ,
152+ width = NULL , just = NULL
136153 )
137154 flip_data(data , params $ flipped_aes )
138155 },
0 commit comments