Skip to content

Commit 58dc414

Browse files
karawoohadley
authored andcommitted
Rename calc() to stat() (#2620)
1 parent d7d18e8 commit 58dc414

36 files changed

+76
-76
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ export(autolayer)
240240
export(autoplot)
241241
export(benchplot)
242242
export(borders)
243-
export(calc)
244243
export(calc_element)
245244
export(combine_vars)
246245
export(continuous_scale)
@@ -504,6 +503,7 @@ export(scale_y_time)
504503
export(sec_axis)
505504
export(set_last_plot)
506505
export(should_stop)
506+
export(stat)
507507
export(stat_bin)
508508
export(stat_bin2d)
509509
export(stat_bin_2d)

NEWS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@
128128
informative error (#2400).
129129

130130
* New syntax for calculated aesthetics. Instead of using `aes(y = ..count..)`
131-
you can (and should!) use `aes(y = calc(count))`. `calc()` is a real function
131+
you can (and should!) use `aes(y = stat(count))`. `stat()` is a real function
132132
with documentation which hopefully will make this part of ggplot2 less
133133
confusing (#2059).
134134

135-
`calc()` is particularly nice for more complex calculations because you
136-
only need to specify it once: `aes(y = calc(count / max(count)))`,
135+
`stat()` is particularly nice for more complex calculations because you
136+
only need to specify it once: `aes(y = stat(count / max(count)))`,
137137
rather than `aes(y = ..count.. / max(..count..))`
138138

139139
* New `tag` label for adding identification tags to plots, typically used for

R/aes-calculated.r

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#' however, you want to map from variables computed by the aesthetic. The
55
#' most common example of this is the height of bars in [geom_histogram()]:
66
#' the height does not come from a variable in the underlying data, but
7-
#' is instead mapped to the `count` computed by [stat_bin()]. The `calc()`
8-
#' function is a flag to ggplot2 to it that you want to use
9-
#' __calculated__ aesthetics produced by the statistic.
7+
#' is instead mapped to the `count` computed by [stat_bin()]. The `stat()`
8+
#' function is a flag to ggplot2 to it that you want to use calculated
9+
#' aesthetics produced by the statistic.
1010
#'
1111
#' This replaces the older approach of surrounding the variable name with
1212
#' `..`.
@@ -16,12 +16,12 @@
1616
#' @examples
1717
#' # Default histogram display
1818
#' ggplot(mpg, aes(displ)) +
19-
#' geom_histogram(aes(y = calc(count)))
19+
#' geom_histogram(aes(y = stat(count)))
2020
#'
2121
#' # Scale tallest bin to 1
2222
#' ggplot(mpg, aes(displ)) +
23-
#' geom_histogram(aes(y = calc(count / max(count))))
24-
calc <- function(x) {
23+
#' geom_histogram(aes(y = stat(count / max(count))))
24+
stat <- function(x) {
2525
x
2626
}
2727

@@ -43,7 +43,7 @@ is_calculated <- function(x) {
4343
} else if (is.symbol(x)) {
4444
is_dotted_var(as.character(x))
4545
} else if (is.call(x)) {
46-
if (identical(x[[1]], quote(calc))) {
46+
if (identical(x[[1]], quote(stat))) {
4747
TRUE
4848
} else {
4949
any(vapply(x, is_calculated, logical(1)))
@@ -67,7 +67,7 @@ strip_dots <- function(expr) {
6767
expr
6868
}
6969
} else if (is.call(expr)) {
70-
if (identical(expr[[1]], quote(calc))) {
70+
if (identical(expr[[1]], quote(stat))) {
7171
strip_dots(expr[[2]])
7272
} else {
7373
expr[-1] <- lapply(expr[-1], strip_dots)

R/aes-group-order.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#' f + geom_line(aes(linetype = variable))
4040
#'
4141
#' # Using facets
42-
#' k <- ggplot(diamonds, aes(carat, calc(density))) + geom_histogram(binwidth = 0.2)
42+
#' k <- ggplot(diamonds, aes(carat, stat(density))) + geom_histogram(binwidth = 0.2)
4343
#' k + facet_grid(. ~ cut)
4444
#'
4545
#' # There are three common cases where the default is not enough, and we

R/geom-contour.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#' v + geom_contour(binwidth = 0.001)
3636
#'
3737
#' # Other parameters
38-
#' v + geom_contour(aes(colour = calc(level)))
38+
#' v + geom_contour(aes(colour = stat(level)))
3939
#' v + geom_contour(colour = "red")
4040
#' v + geom_raster(aes(fill = density)) +
4141
#' geom_contour(colour = "white")

R/geom-count.r

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030
#' # Specifying geom_count without a group identifier leads to a plot which is
3131
#' # not useful:
3232
#' d <- ggplot(diamonds, aes(x = cut, y = clarity))
33-
#' d + geom_count(aes(size = calc(prop)))
33+
#' d + geom_count(aes(size = stat(prop)))
3434
#' # To correct this problem and achieve a more desirable plot, we need
3535
#' # to specify which group the proportion is to be calculated over.
36-
#' d + geom_count(aes(size = calc(prop), group = 1)) +
36+
#' d + geom_count(aes(size = stat(prop), group = 1)) +
3737
#' scale_size_area(max_size = 10)
3838
#'
3939
#' # Or group by x/y variables to have rows/columns sum to 1.
40-
#' d + geom_count(aes(size = calc(prop), group = cut)) +
40+
#' d + geom_count(aes(size = stat(prop), group = cut)) +
4141
#' scale_size_area(max_size = 10)
42-
#' d + geom_count(aes(size = calc(prop), group = clarity)) +
42+
#' d + geom_count(aes(size = stat(prop), group = clarity)) +
4343
#' scale_size_area(max_size = 10)
4444
geom_count <- function(mapping = NULL, data = NULL,
4545
stat = "sum", position = "identity",

R/geom-density.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
#' ggplot(diamonds, aes(carat, fill = cut)) +
3939
#' geom_density(position = "stack")
4040
#' # Preserves marginal densities
41-
#' ggplot(diamonds, aes(carat, calc(count), fill = cut)) +
41+
#' ggplot(diamonds, aes(carat, stat(count), fill = cut)) +
4242
#' geom_density(position = "stack")
4343
#'
4444
#' # You can use position="fill" to produce a conditional density estimate
45-
#' ggplot(diamonds, aes(carat, calc(count), fill = cut)) +
45+
#' ggplot(diamonds, aes(carat, stat(count), fill = cut)) +
4646
#' geom_density(position = "fill")
4747
#' }
4848
geom_density <- function(mapping = NULL, data = NULL,

R/geom-density2d.r

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#' ylim(40, 110)
2222
#' m + geom_density_2d()
2323
#' \donttest{
24-
#' m + stat_density_2d(aes(fill = calc(level)), geom = "polygon")
24+
#' m + stat_density_2d(aes(fill = stat(level)), geom = "polygon")
2525
#'
2626
#' set.seed(4393)
2727
#' dsmall <- diamonds[sample(nrow(diamonds), 1000), ]
@@ -31,9 +31,9 @@
3131
#' d + geom_density_2d(aes(colour = cut))
3232
#'
3333
#' # If we turn contouring off, we can use use geoms like tiles:
34-
#' d + stat_density_2d(geom = "raster", aes(fill = calc(density)), contour = FALSE)
34+
#' d + stat_density_2d(geom = "raster", aes(fill = stat(density)), contour = FALSE)
3535
#' # Or points:
36-
#' d + stat_density_2d(geom = "point", aes(size = calc(density)), n = 20, contour = FALSE)
36+
#' d + stat_density_2d(geom = "point", aes(size = stat(density)), n = 20, contour = FALSE)
3737
#' }
3838
geom_density_2d <- function(mapping = NULL, data = NULL,
3939
stat = "density2d", position = "identity",

R/geom-histogram.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#'
4242
#' # To make it easier to compare distributions with very different counts,
4343
#' # put density on the y axis instead of the default count
44-
#' ggplot(diamonds, aes(price, calc(density), colour = cut)) +
44+
#' ggplot(diamonds, aes(price, stat(density), colour = cut)) +
4545
#' geom_freqpoly(binwidth = 500)
4646
#'
4747
#' if (require("ggplot2movies")) {

R/geom-tile.r

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848
#' # Inspired by the image-density plots of Ken Knoblauch
4949
#' cars <- ggplot(mtcars, aes(mpg, factor(cyl)))
5050
#' cars + geom_point()
51-
#' cars + stat_bin2d(aes(fill = calc(count)), binwidth = c(3,1))
52-
#' cars + stat_bin2d(aes(fill = calc(density)), binwidth = c(3,1))
51+
#' cars + stat_bin2d(aes(fill = stat(count)), binwidth = c(3,1))
52+
#' cars + stat_bin2d(aes(fill = stat(density)), binwidth = c(3,1))
5353
#'
54-
#' cars + stat_density(aes(fill = calc(density)), geom = "raster", position = "identity")
55-
#' cars + stat_density(aes(fill = calc(count)), geom = "raster", position = "identity")
54+
#' cars + stat_density(aes(fill = stat(density)), geom = "raster", position = "identity")
55+
#' cars + stat_density(aes(fill = stat(count)), geom = "raster", position = "identity")
5656
#' }
5757
geom_tile <- function(mapping = NULL, data = NULL,
5858
stat = "identity", position = "identity",

0 commit comments

Comments
 (0)