40
40
# ' automatically determines the orientation from the aesthetic mapping. In the
41
41
# ' rare event that this fails it can be given explicitly by setting `orientation`
42
42
# ' 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.
44
50
# ' @param geom,stat Override the default connection between `geom_bar()` and
45
51
# ' `stat_count()`.
46
52
# ' @examples
80
86
# ' ggplot(df, aes(x)) + geom_bar()
81
87
# ' # cf. a histogram of the same data
82
88
# ' 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)
83
96
geom_bar <- function (mapping = NULL , data = NULL ,
84
97
stat = " count" , position = " stack" ,
85
98
... ,
99
+ just = 0.5 ,
86
100
width = NULL ,
87
101
na.rm = FALSE ,
88
102
orientation = NA ,
@@ -97,6 +111,7 @@ geom_bar <- function(mapping = NULL, data = NULL,
97
111
show.legend = show.legend ,
98
112
inherit.aes = inherit.aes ,
99
113
params = list2(
114
+ just = just ,
100
115
width = width ,
101
116
na.rm = na.rm ,
102
117
orientation = orientation ,
@@ -123,16 +138,18 @@ GeomBar <- ggproto("GeomBar", GeomRect,
123
138
params
124
139
},
125
140
126
- extra_params = c(" na.rm" , " orientation" ),
141
+ extra_params = c(" just " , " na.rm" , " orientation" ),
127
142
128
143
setup_data = function (data , params ) {
129
144
data $ flipped_aes <- params $ flipped_aes
130
145
data <- flip_data(data , params $ flipped_aes )
131
146
data $ width <- data $ width %|| %
132
147
params $ width %|| % (resolution(data $ x , FALSE ) * 0.9 )
148
+ data $ just <- params $ just %|| % 0.5
133
149
data <- transform(data ,
134
150
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
136
153
)
137
154
flip_data(data , params $ flipped_aes )
138
155
},
0 commit comments