Skip to content

Commit dd82e4a

Browse files
committed
Added geom_col() & updated references & examples in other functions. Substituted geom_col() for geom_bar() in one test to ensure it had test coverage outside the examples.
1 parent 59c503b commit dd82e4a

22 files changed

+227
-35
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Collate:
100100
'geom-bin2d.r'
101101
'geom-blank.r'
102102
'geom-boxplot.r'
103+
'geom-col.r'
103104
'geom-path.r'
104105
'geom-contour.r'
105106
'geom-count.r'

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export(GeomArea)
130130
export(GeomBar)
131131
export(GeomBlank)
132132
export(GeomBoxplot)
133+
export(GeomCol)
133134
export(GeomContour)
134135
export(GeomCrossbar)
135136
export(GeomCurve)
@@ -269,6 +270,7 @@ export(geom_bar)
269270
export(geom_bin2d)
270271
export(geom_blank)
271272
export(geom_boxplot)
273+
export(geom_col)
272274
export(geom_contour)
273275
export(geom_count)
274276
export(geom_crossbar)

NEWS.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
default. The `caption` annotation below the plot will be right-justified
1515
by default. (@hrbrmstr)
1616

17-
* Minor code formatting issues in examples and function parameters were
18-
fixed. (@hrbrmstr)
17+
* Minor code formatting and grammar issues in examples and function
18+
parameters were fixed. (@hrbrmstr)
19+
20+
* `geom_col()` was added to complement `geom_bar()`. It uses `stat="identity"`
21+
by default, making the `y` aesthetic mandatory. It does not support any
22+
other `stat_()` and does not provide fallback support for the `binwidth`
23+
parameter. Examples and references in other functions were updated to
24+
demonstrate `geom_col()` usage. (@hrbrmstr)
1925

2026
# ggplot2 2.1.0
2127

R/coord-polar.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#' value = c(20, 80)
3737
#' )
3838
#' ggplot(df, aes(x = "", y = value, fill = variable)) +
39-
#' geom_bar(width = 1, stat = "identity") +
39+
#' geom_col(width = 1) +
4040
#' scale_fill_manual(values = c("red", "yellow")) +
4141
#' coord_polar("y", start = pi / 3) +
4242
#' labs(title = "Pac man")

R/fortify-lm.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
#'
5151
#' plot(mod, which = 4)
5252
#' ggplot(mod, aes(seq_along(.cooksd), .cooksd)) +
53-
#' geom_bar(stat = "identity")
53+
#' geom_col()
5454
#'
5555
#' plot(mod, which = 5)
5656
#' ggplot(mod, aes(.hat, .stdresid)) +

R/geom-bar.r

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' There are two types of bar charts, determined by what is mapped to bar
44
#' height. By default, \code{geom_bar} uses \code{stat="count"} which makes the
5-
#' height of the bar proportion to the number of cases in each group (or if the
5+
#' height of the bar proportional to the number of cases in each group (or if the
66
#' \code{weight} aethetic is supplied, the sum of the weights). If you want the
77
#' heights of the bars to represent values in the data, use
88
#' \code{stat="identity"} and map a variable to the \code{y} aesthetic.
@@ -23,7 +23,8 @@
2323
#' @section Aesthetics:
2424
#' \Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "bar")}
2525
#'
26-
#' @seealso \code{\link{geom_histogram}} for continuous data,
26+
#' @seealso \code{\link{geom_col}} which uses \code{stat="identity"} by default,
27+
#' \code{\link{geom_histogram}} for continuous data,
2728
#' \code{\link{position_dodge}} for creating side-by-side barcharts.
2829
#' @export
2930
#' @inheritParams layer
@@ -47,7 +48,10 @@
4748
#' df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
4849
#' ggplot(df, aes(trt, outcome)) +
4950
#' geom_bar(stat = "identity")
50-
#' # But geom_point() display exactly the same information and doesn't
51+
#' # And, even more succinctly with geom_col()
52+
#' ggplot(df, aes(trt, outcome)) +
53+
#' geom_col()
54+
#' # But geom_point() displays exactly the same information and doesn't
5155
#' # require the y-axis to touch zero.
5256
#' ggplot(df, aes(trt, outcome)) +
5357
#' geom_point()

R/geom-col.r

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#' Bars, rectangles with bases on x-axis
2+
#'
3+
#' This is an alternate version of \code{geom_bar} that uses
4+
#' \code{stat="identity"} (i.e. the heights of the bars represent values in the
5+
#' data) by default, requiring the use of a \code{y} aesthetic.
6+
#' If you need \code{stat="count"} functionality, use \code{geom_bar}.
7+
#'
8+
#' A bar chart maps the height of the bar to a variable, and so the base of the
9+
#' bar must always be shown to produce a valid visual comparison. Naomi Robbins
10+
#' has a nice
11+
#' \href{http://www.b-eye-network.com/view/index.php?cid=2468}{article on this
12+
#' topic}. This is why it doesn't make sense to use a log-scaled y axis with a
13+
#' bar chart.
14+
#'
15+
#' By default, multiple x's occurring in the same place will be stacked atop one
16+
#' another by \code{\link{position_stack}}. If you want them to be dodged
17+
#' side-to-side, see \code{\link{position_dodge}}. Finally,
18+
#' \code{\link{position_fill}} shows relative proportions at each x by stacking
19+
#' the bars and then stretching or squashing to the same height.
20+
#'
21+
#' @section Aesthetics:
22+
#' \Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "col")}
23+
#'
24+
#' @seealso \code{\link{geom_bar}} to make the height of the bar proportional to
25+
#' the number of cases in each group or sum of weights,
26+
#' \code{\link{geom_histogram}} for continuous data,
27+
#' \code{\link{position_dodge}} for creating side-by-side barcharts.
28+
#' @export
29+
#' @inheritParams layer
30+
#' @inheritParams geom_point
31+
#' @param width Bar width. By default, set to 90\% of the resolution of the data.
32+
#' @examples
33+
#' df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
34+
#' ggplot(df, aes(trt, outcome)) +
35+
#' geom_col()
36+
#' # But geom_point() displays exactly the same information and doesn't
37+
#' # require the y-axis to touch zero.
38+
#' ggplot(df, aes(trt, outcome)) +
39+
#' geom_point()
40+
geom_col <- function(mapping = NULL, data = NULL,
41+
position = "stack",
42+
...,
43+
width = NULL,
44+
na.rm = FALSE,
45+
show.legend = NA,
46+
inherit.aes = TRUE) {
47+
48+
layer(
49+
data = data,
50+
mapping = mapping,
51+
stat = "identity",
52+
geom = GeomCol,
53+
position = position,
54+
show.legend = show.legend,
55+
inherit.aes = inherit.aes,
56+
params = list(
57+
width = width,
58+
na.rm = na.rm,
59+
...
60+
)
61+
)
62+
}
63+
64+
#' @rdname ggplot2-ggproto
65+
#' @format NULL
66+
#' @usage NULL
67+
#' @export
68+
#' @include geom-rect.r
69+
GeomCol <- ggproto("GeomCol", GeomRect,
70+
required_aes = c("x", "y"),
71+
72+
setup_data = function(data, params) {
73+
data$width <- data$width %||%
74+
params$width %||% (resolution(data$x, FALSE) * 0.9)
75+
transform(data,
76+
ymin = pmin(y, 0), ymax = pmax(y, 0),
77+
xmin = x - width / 2, xmax = x + width / 2, width = NULL
78+
)
79+
},
80+
81+
draw_panel = function(self, data, panel_scales, coord, width = NULL) {
82+
# Hack to ensure that width is detected as a parameter
83+
ggproto_parent(GeomRect, self)$draw_panel(data, panel_scales, coord)
84+
}
85+
)

R/geom-linerange.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@
4040
#' # specify the dodge width
4141
#' p <- ggplot(df, aes(trt, resp, fill = group))
4242
#' p +
43-
#' geom_bar(position = "dodge", stat = "identity") +
43+
#' geom_col(position = "dodge") +
4444
#' geom_errorbar(aes(ymin = lower, ymax = upper), position = "dodge", width = 0.25)
4545
#'
4646
#' # Because the bars and errorbars have different widths
4747
#' # we need to specify how wide the objects we are dodging are
4848
#' dodge <- position_dodge(width=0.9)
4949
#' p +
50-
#' geom_bar(position = dodge, stat = "identity") +
50+
#' geom_col(position = dodge) +
5151
#' geom_errorbar(aes(ymin = lower, ymax = upper), position = dodge, width = 0.25)
5252
geom_linerange <- function(mapping = NULL, data = NULL,
5353
stat = "identity", position = "identity",

R/geom-text.r

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,23 @@
8787
#' # ggplot2 doesn't know you want to give the labels the same virtual width
8888
#' # as the bars:
8989
#' ggplot(data = df, aes(x, y, fill = grp, label = y)) +
90-
#' geom_bar(stat = "identity", position = "dodge") +
90+
#' geom_col(position = "dodge") +
9191
#' geom_text(position = "dodge")
9292
#' # So tell it:
9393
#' ggplot(data = df, aes(x, y, fill = grp, label = y)) +
94-
#' geom_bar(stat = "identity", position = "dodge") +
94+
#' geom_col(position = "dodge") +
9595
#' geom_text(position = position_dodge(0.9))
9696
#' # Use you can't nudge and dodge text, so instead adjust the y postion
9797
#' ggplot(data = df, aes(x, y, fill = grp, label = y)) +
98-
#' geom_bar(stat = "identity", position = "dodge") +
98+
#' geom_col(position = "dodge") +
9999
#' geom_text(aes(y = y + 0.05), position = position_dodge(0.9), vjust = 0)
100100
#'
101101
#' # To place text in the middle of each bar in a stacked barplot, you
102102
#' # need to do the computation yourself
103103
#' df <- transform(df, mid_y = ave(df$y, df$x, FUN = function(val) cumsum(val) - (0.5 * val)))
104104
#'
105105
#' ggplot(data = df, aes(x, y, fill = grp, label = y)) +
106-
#' geom_bar(stat = "identity") +
106+
#' geom_col() +
107107
#' geom_text(aes(y = mid_y))
108108
#'
109109
#' # Justification -------------------------------------------------------------

R/position-dodge.r

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
#' # To dodge items with different widths, you need to be explicit
1818
#' df <- data.frame(x = c("a","a","b","b"), y = 2:5, g = rep(1:2, 2))
1919
#' p <- ggplot(df, aes(x, y, group = g)) +
20-
#' geom_bar(
21-
#' stat = "identity", position = "dodge",
22-
#' fill = "grey50", colour = "black"
20+
#' geom_col(
21+
#' position = "dodge", fill = "grey50", colour = "black"
2322
#' )
2423
#' p
2524
#'

R/position-stack.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#' height.
55
#'
66
#' @family position adjustments
7-
#' @seealso See \code{\link{geom_bar}} and \code{\link{geom_area}} for
7+
#' @seealso See \code{\link{geom_bar}}, and \code{\link{geom_area}} for
88
#' more examples.
99
#' @export
1010
#' @examples

man/coord_polar.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fortify.lm.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_bar.Rd

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_col.Rd

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_linerange.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)