Skip to content

Commit 4668ce7

Browse files
committed
Consistent approach to na.rm.
Fixes #1305
1 parent ce58b2d commit 4668ce7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+486
-269
lines changed

R/annotation-custom.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ annotation_custom <- function(grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax =
6565
#' @usage NULL
6666
#' @export
6767
GeomCustomAnn <- ggproto("GeomCustomAnn", Geom,
68+
extra_params = "",
69+
handle_na = function(data, params) {
70+
data
71+
},
72+
6873
draw_panel = function(data, panel_scales, coord, grob, xmin, xmax,
6974
ymin, ymax) {
7075
if (!inherits(coord, "CoordCartesian")) {

R/annotation-logticks.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ annotation_logticks <- function(base = 10, sides = "bl", scaled = TRUE,
109109
#' @usage NULL
110110
#' @export
111111
GeomLogticks <- ggproto("GeomLogticks", Geom,
112+
extra_params = "",
113+
handle_na = function(data, params) {
114+
data
115+
},
116+
112117
draw_panel = function(data, panel_scales, coord, base = 10, sides = "bl",
113118
scaled = TRUE, short = unit(0.1, "cm"), mid = unit(0.2, "cm"),
114119
long = unit(0.3, "cm"))

R/annotation-map.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ annotation_map <- function(map, ...) {
4848
#' @usage NULL
4949
#' @export
5050
GeomAnnotationMap <- ggproto("GeomAnnotationMap", GeomMap,
51+
extra_params = "",
52+
handle_na = function(data, params) {
53+
data
54+
},
55+
5156
draw_panel = function(data, panel_scales, coord, map) {
5257
# Munch, then set up id variable for polygonGrob -
5358
# must be sequential integers

R/annotation-raster.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ annotation_raster <- function(raster, xmin, xmax, ymin, ymax,
6666
#' @usage NULL
6767
#' @export
6868
GeomRasterAnn <- ggproto("GeomRasterAnn", GeomRaster,
69+
extra_params = "",
70+
handle_na = function(data, params) {
71+
data
72+
},
73+
6974
draw_panel = function(data, panel_scales, coord, raster, xmin, xmax,
7075
ymin, ymax, interpolate = FALSE) {
7176
if (!inherits(coord, "CoordCartesian")) {

R/annotation.r

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#' you must specify at least one of these.
1717
#' @param ... other aesthetics. These are not scaled so you can do (e.g.)
1818
#' \code{colour = "red"} to get a red point.
19+
#' @inheritParams geom_point
1920
#' @export
2021
#' @examples
2122
#' p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
@@ -30,7 +31,8 @@
3031
#'
3132
#' p + annotate("text", x = 2:3, y = 20:21, label = c("my label", "label 2"))
3233
annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
33-
ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, ...) {
34+
ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, ...,
35+
na.rm = FALSE) {
3436

3537
position <- compact(list(
3638
x = x, xmin = xmin, xmax = xmax, xend = xend,
@@ -51,7 +53,10 @@ annotate <- function(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
5153
data <- data.frame(position)
5254
layer(
5355
geom = geom,
54-
params = list(...),
56+
params = list(
57+
na.rm = na.rm,
58+
...
59+
),
5560
stat = StatIdentity,
5661
position = PositionIdentity,
5762
data = data,

R/geom-.r

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Geom <- ggproto("Geom",
6060
draw_key = draw_key_point,
6161

6262
handle_na = function(self, data, params) {
63-
remove_missing(data, isTRUE(params$na.rm),
63+
remove_missing(data, params$na.rm,
6464
c(self$required_aes, self$non_missing_aes),
6565
snake_class(self)
6666
)
@@ -69,6 +69,9 @@ Geom <- ggproto("Geom",
6969
draw_layer = function(self, data, params, panel, coord) {
7070
if (empty(data)) return(list(zeroGrob()))
7171

72+
# Trim off extra parameters
73+
params <- params[intersect(names(params), self$parameters())]
74+
7275
args <- c(list(quote(data), quote(panel_scales), quote(coord)), params)
7376
plyr::dlply(data, "PANEL", function(data) {
7477
if (empty(data)) return(zeroGrob())
@@ -112,7 +115,14 @@ Geom <- ggproto("Geom",
112115
data
113116
},
114117

115-
parameters = function(self) {
118+
# Most parameters for the geom are taken automatically from draw_panel() or
119+
# draw_groups(). However, some additional parameters may be needed
120+
# for setup_data() or handle_na(). These can not be imputed automatically,
121+
# so the slightly hacky "extra_params" field is used instead. By
122+
# default it contains `na.rm`
123+
extra_params = c("na.rm"),
124+
125+
parameters = function(self, extra = FALSE) {
116126
# Look first in draw_panel. If it contains ... then look in draw groups
117127
panel_args <- names(ggproto_formals(self$draw_panel))
118128
group_args <- names(ggproto_formals(self$draw_group))
@@ -121,6 +131,9 @@ Geom <- ggproto("Geom",
121131
# Remove arguments of defaults
122132
args <- setdiff(args, names(ggproto_formals(Geom$draw_group)))
123133

134+
if (extra) {
135+
args <- union(args, self$extra_params)
136+
}
124137
args
125138
},
126139

R/geom-abline.r

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ NULL
6767
#' geom_point() +
6868
#' geom_hline(aes(yintercept = wt, colour = wt), mean_wt) +
6969
#' facet_wrap(~ cyl)
70-
geom_abline <- function(mapping = NULL, data = NULL, show.legend = NA, ...,
71-
slope, intercept) {
70+
geom_abline <- function(mapping = NULL, data = NULL,
71+
...,
72+
slope, intercept,
73+
na.rm = FALSE, show.legend = NA) {
7274

7375
# If nothing set, default to y = x
7476
if (missing(mapping) && missing(slope) && missing(intercept)) {
@@ -94,7 +96,10 @@ geom_abline <- function(mapping = NULL, data = NULL, show.legend = NA, ...,
9496
position = PositionIdentity,
9597
show.legend = show.legend,
9698
inherit.aes = FALSE,
97-
params = list(...)
99+
params = list(
100+
na.rm = na.rm,
101+
...
102+
)
98103
)
99104
}
100105

R/geom-bar.r

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
#' }
7373
geom_bar <- function(mapping = NULL, data = NULL, stat = "count",
7474
position = "stack", width = NULL, ...,
75-
show.legend = NA, inherit.aes = TRUE) {
75+
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) {
7676
layer(
7777
data = data,
7878
mapping = mapping,
@@ -83,6 +83,7 @@ geom_bar <- function(mapping = NULL, data = NULL, stat = "count",
8383
inherit.aes = inherit.aes,
8484
params = list(
8585
width = width,
86+
na.rm = na.rm,
8687
...
8788
)
8889
)

R/geom-bin2d.r

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
#' # Or by specifying the width of the bins
2121
#' d + geom_bin2d(binwidth = c(0.1, 0.1))
2222
geom_bin2d <- function(mapping = NULL, data = NULL, stat = "bin2d",
23-
position = "identity", show.legend = NA, inherit.aes = TRUE, ...) {
23+
position = "identity", na.rm = FALSE,
24+
show.legend = NA, inherit.aes = TRUE, ...) {
2425

2526
layer(
2627
data = data,
@@ -30,6 +31,9 @@ geom_bin2d <- function(mapping = NULL, data = NULL, stat = "bin2d",
3031
position = position,
3132
show.legend = show.legend,
3233
inherit.aes = inherit.aes,
33-
params = list(...)
34+
params = list(
35+
na.rm = na.rm,
36+
...
37+
)
3438
)
3539
}

R/geom-blank.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ geom_blank <- function(mapping = NULL, data = NULL, stat = "identity",
4343
#' @export
4444
GeomBlank <- ggproto("GeomBlank", Geom,
4545
default_aes = aes(),
46+
handle_na = function(data, params) data,
4647
draw_panel = function(...) nullGrob()
4748
)

R/geom-boxplot.r

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@
8686
#' )
8787
#' }
8888
geom_boxplot <- function(mapping = NULL, data = NULL, stat = "boxplot",
89-
position = "dodge", outlier.colour = "black", outlier.shape = 19,
90-
outlier.size = 1.5, outlier.stroke = 0.5, notch = FALSE, notchwidth = 0.5,
91-
varwidth = FALSE, show.legend = NA, inherit.aes = TRUE, ...)
92-
{
89+
position = "dodge", outlier.colour = "black",
90+
outlier.shape = 19, outlier.size = 1.5,
91+
outlier.stroke = 0.5, notch = FALSE, notchwidth = 0.5,
92+
varwidth = FALSE, na.rm = FALSE,
93+
show.legend = NA, inherit.aes = TRUE, ...) {
9394
layer(
9495
data = data,
9596
mapping = mapping,
@@ -106,6 +107,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL, stat = "boxplot",
106107
notch = notch,
107108
notchwidth = notchwidth,
108109
varwidth = varwidth,
110+
na.rm = na.rm,
109111
...
110112
)
111113
)

R/geom-count.r

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@
4141
#' d + geom_count(aes(size = ..prop.., group = clarity)) +
4242
#' scale_size_area(max_size = 10)
4343
geom_count <- function(mapping = NULL, data = NULL, stat = "sum",
44-
position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE,
45-
...)
46-
{
44+
position = "identity", na.rm = FALSE,
45+
show.legend = NA, inherit.aes = TRUE, ...) {
4746
layer(
4847
data = data,
4948
mapping = mapping,
@@ -52,6 +51,9 @@ geom_count <- function(mapping = NULL, data = NULL, stat = "sum",
5251
position = position,
5352
show.legend = show.legend,
5453
inherit.aes = inherit.aes,
55-
params = list(...)
54+
params = list(
55+
na.rm = na.rm,
56+
...
57+
)
5658
)
5759
}

R/geom-crossbar.r

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#' @export
22
#' @rdname geom_linerange
33
geom_crossbar <- function(mapping = NULL, data = NULL, stat = "identity",
4-
position = "identity", fatten = 2.5, show.legend = NA,
5-
inherit.aes = TRUE, ...) {
4+
position = "identity", fatten = 2.5, na.rm = FALSE,
5+
show.legend = NA, inherit.aes = TRUE, ...) {
66
layer(
77
data = data,
88
mapping = mapping,
@@ -13,6 +13,7 @@ geom_crossbar <- function(mapping = NULL, data = NULL, stat = "identity",
1313
inherit.aes = inherit.aes,
1414
params = list(
1515
fatten = fatten,
16+
na.rm = na.rm,
1617
...
1718
)
1819
)

R/geom-curve.r

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
#' @export
33
#' @rdname geom_segment
44
geom_curve <- function(mapping = NULL, data = NULL, stat = "identity",
5-
position = "identity", curvature = 0.5, angle = 90, ncp = 5, arrow = NULL,
6-
lineend = "butt", na.rm = FALSE, inherit.aes = TRUE, ...)
7-
{
5+
position = "identity", curvature = 0.5, angle = 90,
6+
ncp = 5, arrow = NULL, lineend = "butt",
7+
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...) {
88
layer(
99
data = data,
1010
mapping = mapping,
1111
stat = stat,
1212
geom = GeomCurve,
1313
position = position,
14+
show.legend = show.legend,
1415
inherit.aes = inherit.aes,
1516
params = list(
1617
arrow = arrow,

R/geom-density.r

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
#' geom_density(position = "fill")
4747
#' }
4848
geom_density <- function(mapping = NULL, data = NULL, stat = "density",
49-
position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE,
50-
...) {
49+
position = "identity", na.rm = FALSE,
50+
show.legend = NA, inherit.aes = TRUE, ...) {
5151

5252
layer(
5353
data = data,
@@ -57,7 +57,10 @@ geom_density <- function(mapping = NULL, data = NULL, stat = "density",
5757
position = position,
5858
show.legend = show.legend,
5959
inherit.aes = inherit.aes,
60-
params = list(..., na.rm = na.rm)
60+
params = list(
61+
na.rm = na.rm,
62+
...
63+
)
6164
)
6265
}
6366

R/geom-density2d.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
geom_density2d <- function(mapping = NULL, data = NULL, stat = "density2d",
3838
position = "identity", lineend = "butt",
3939
linejoin = "round", linemitre = 1,
40-
na.rm = FALSE, show.legend = NA,
41-
inherit.aes = TRUE, ...) {
40+
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE,
41+
...) {
4242
layer(
4343
data = data,
4444
mapping = mapping,

R/geom-dotplot.r

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
#' on the right (a, b], or not [a, b)
4444
#' @param width When \code{binaxis} is "y", the spacing of the dot stacks
4545
#' for dodging.
46-
#' @param na.rm If \code{FALSE} (the default), removes missing values with
47-
#' a warning. If \code{TRUE} silently removes missing values.
4846
#' @param drop If TRUE, remove all bins with zero counts
4947
#' @section Computed variables:
5048
#' \describe{
@@ -114,12 +112,12 @@
114112
#' geom_dotplot(binaxis = "y", stackgroups = TRUE, binwidth = 1, method = "histodot")
115113
#' }
116114
geom_dotplot <- function(mapping = NULL, data = NULL,
117-
position = "identity", na.rm = FALSE, binwidth = NULL, binaxis = "x",
118-
method = "dotdensity", binpositions = "bygroup", stackdir = "up",
119-
stackratio = 1, dotsize = 1, stackgroups = FALSE,
120-
origin = NULL, right = TRUE, width = 0.9, drop = FALSE, show.legend = NA,
121-
inherit.aes = TRUE, ...)
122-
{
115+
position = "identity", binwidth = NULL, binaxis = "x",
116+
method = "dotdensity", binpositions = "bygroup",
117+
stackdir = "up", stackratio = 1, dotsize = 1,
118+
stackgroups = FALSE, origin = NULL, right = TRUE,
119+
width = 0.9, drop = FALSE, na.rm = FALSE,
120+
show.legend = NA, inherit.aes = TRUE, ...) {
123121
# If identical(position, "stack") or position is position_stack(), tell them
124122
# to use stackgroups=TRUE instead. Need to use identical() instead of ==,
125123
# because == will fail if object is position_stack() or position_dodge()
@@ -141,7 +139,6 @@ geom_dotplot <- function(mapping = NULL, data = NULL,
141139
# Need to make sure that the binaxis goes to both the stat and the geom
142140
params = list(
143141
binaxis = binaxis,
144-
na.rm = na.rm,
145142
binwidth = binwidth,
146143
binpositions = binpositions,
147144
method = method,
@@ -153,6 +150,7 @@ geom_dotplot <- function(mapping = NULL, data = NULL,
153150
stackratio = stackratio,
154151
dotsize = dotsize,
155152
stackgroups = stackgroups,
153+
na.rm = na.rm,
156154
...
157155
)
158156
)

R/geom-errorbar.r

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#' @export
22
#' @rdname geom_linerange
33
geom_errorbar <- function(mapping = NULL, data = NULL, stat = "identity",
4-
position = "identity", show.legend = NA,
5-
inherit.aes = TRUE, ...) {
4+
position = "identity", na.rm = FALSE,
5+
show.legend = NA, inherit.aes = TRUE, ...) {
66
layer(
77
data = data,
88
mapping = mapping,
@@ -11,7 +11,10 @@ geom_errorbar <- function(mapping = NULL, data = NULL, stat = "identity",
1111
position = position,
1212
show.legend = show.legend,
1313
inherit.aes = inherit.aes,
14-
params = list(...)
14+
params = list(
15+
na.rm = na.rm,
16+
...
17+
)
1518
)
1619
}
1720

0 commit comments

Comments
 (0)