Skip to content

Commit 13be8a5

Browse files
committed
Merge remote-tracking branch 'hadley/master' into function_as_data
# Conflicts: # NEWS.md
2 parents 6510c45 + 7be4c89 commit 13be8a5

17 files changed

+171
-66
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Collate:
170170
'scale-manual.r'
171171
'scale-shape.r'
172172
'scale-size.r'
173+
'scale-type.R'
173174
'scales-.r'
174175
'stat-bin.r'
175176
'stat-bin2d.r'

NAMESPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ S3method(scale_type,default)
104104
S3method(scale_type,factor)
105105
S3method(scale_type,logical)
106106
S3method(scale_type,numeric)
107+
S3method(scale_type,ordered)
107108
S3method(str,uneval)
108109
S3method(summary,ggplot)
109110
S3method(widthDetails,stripGrob)
@@ -373,6 +374,8 @@ export(scale_color_identity)
373374
export(scale_color_manual)
374375
export(scale_colour_brewer)
375376
export(scale_colour_continuous)
377+
export(scale_colour_date)
378+
export(scale_colour_datetime)
376379
export(scale_colour_discrete)
377380
export(scale_colour_distiller)
378381
export(scale_colour_gradient)
@@ -384,6 +387,8 @@ export(scale_colour_identity)
384387
export(scale_colour_manual)
385388
export(scale_fill_brewer)
386389
export(scale_fill_continuous)
390+
export(scale_fill_date)
391+
export(scale_fill_datetime)
387392
export(scale_fill_discrete)
388393
export(scale_fill_distiller)
389394
export(scale_fill_gradient)
@@ -407,6 +412,8 @@ export(scale_shape_manual)
407412
export(scale_size)
408413
export(scale_size_area)
409414
export(scale_size_continuous)
415+
export(scale_size_date)
416+
export(scale_size_datetime)
410417
export(scale_size_discrete)
411418
export(scale_size_identity)
412419
export(scale_size_manual)

NEWS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
applied to the data passed to the `ggplot()` function and must return a
55
data.frame (#1527).
66

7+
* The theme can now modify the margins of legend title and text (#1502).
8+
9+
* `scale_size()` warns when used with categorical data.
10+
11+
* `scale_size()`, `scale_colour()`, and `scale_fill()` gain date and date-time
12+
variants (#1526).
13+
14+
* `stat_function()` gains an `xlim` parameter (#1528).
15+
716
* `stat_summary()` preserves sorted x order which avoids artefacts when
817
display results with `geom_smooth()` (#1520).
918

@@ -16,6 +25,9 @@
1625
* `stat_ecdf()` does a better job of adding padding to -Inf/Inf, and gains
1726
an argument `pad` to suppress the padding if not needed (#1467).
1827

28+
* `theme_void()` was completely void of text but facets and legends still
29+
needed labels. They are now visible (@jiho).
30+
1931
* Multipanel empty data is correctly plotted, rather than throwing an unhelpful
2032
error (#1445).
2133

R/guide-legend.r

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ guide_gengrob.legend <- function(guide, theme) {
300300
guide$title.theme %||% calc_element("legend.title", theme),
301301
label = guide$title,
302302
hjust = guide$title.hjust %||% theme$legend.title.align %||% 0,
303-
vjust = guide$title.vjust %||% 0.5
303+
vjust = guide$title.vjust %||% 0.5,
304+
expand_x = TRUE,
305+
expand_y = TRUE
304306
)
305307
)
306308

@@ -321,7 +323,8 @@ guide_gengrob.legend <- function(guide, theme) {
321323

322324
grob.labels <- lapply(guide$key$.label, function(label, ...) {
323325
g <- element_grob(element = label.theme, label = label,
324-
x = x, y = y, hjust = hjust, vjust = vjust)
326+
x = x, y = y, hjust = hjust, vjust = vjust,
327+
expand_x = TRUE, expand_y = TRUE)
325328
ggname("guide.label", g)
326329
})
327330
}

R/scale-shape.r

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ scale_shape <- function(..., solid = TRUE) {
3030

3131
#' @rdname scale_shape
3232
#' @export
33+
#' @usage NULL
3334
scale_shape_discrete <- scale_shape
3435

3536
#' @rdname scale_shape
3637
#' @export
38+
#' @usage NULL
3739
scale_shape_continuous <- function(...) {
3840
stop("A continuous variable can not be mapped to shape", call. = FALSE)
3941
}

R/scale-size.r

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ scale_size <- scale_size_continuous
6060
#' @export
6161
#' @usage NULL
6262
scale_size_discrete <- function(..., range = c(2, 6)) {
63+
warning("Using size for a discrete variable is not advised.", call. = FALSE)
6364
discrete_scale("size", "size_d", function(n) {
6465
area <- seq(range[1] ^ 2, range[2] ^ 2, length.out = n)
6566
sqrt(area)
@@ -76,3 +77,18 @@ scale_size_area <- function(..., max_size = 6) {
7677
palette = abs_area(max_size),
7778
rescaler = rescale_max, ...)
7879
}
80+
81+
#' @rdname scale_size
82+
#' @export
83+
#' @usage NULL
84+
scale_size_datetime <- function() {
85+
scale_size_continuous(trans = "time")
86+
}
87+
88+
#' @rdname scale_size
89+
#' @export
90+
#' @usage NULL
91+
scale_size_date <- function() {
92+
scale_size_continuous(trans = "date")
93+
}
94+

R/scale-type.R

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
find_scale <- function(aes, x, env = parent.frame()) {
2+
type <- scale_type(x)
3+
candidates <- paste("scale", aes, type, sep = "_")
4+
5+
for (scale in candidates) {
6+
scale_f <- find_global(scale, env, mode = "function")
7+
if (!is.null(scale_f))
8+
return(scale_f())
9+
}
10+
11+
# Failure to find a scale is not an error because some "aesthetics" don't
12+
# need scales (e.g. group), and it allows others to extend ggplot2 with
13+
# their own aesthetics
14+
15+
return(NULL)
16+
}
17+
18+
# Look for object first in parent environment and if not found, then in
19+
# ggplot2 namespace environment. This makes it possible to override default
20+
# scales by setting them in the parent environment.
21+
find_global <- function(name, env, mode = "any") {
22+
if (exists(name, envir = env, mode = mode)) {
23+
return(get(name, envir = env, mode = mode))
24+
}
25+
26+
nsenv <- asNamespace("ggplot2")
27+
if (exists(name, envir = nsenv, mode = mode)) {
28+
return(get(name, envir = nsenv, mode = mode))
29+
}
30+
31+
NULL
32+
}
33+
34+
# Determine default type of a scale
35+
scale_type <- function(x) UseMethod("scale_type")
36+
37+
#' @export
38+
scale_type.default <- function(x) {
39+
message("Don't know how to automatically pick scale for object of type ",
40+
paste(class(x), collapse = "/"), ". Defaulting to continuous.")
41+
"continuous"
42+
}
43+
44+
#' @export
45+
scale_type.AsIs <- function(x) "identity"
46+
47+
#' @export
48+
scale_type.logical <- function(x) "discrete"
49+
50+
#' @export
51+
scale_type.character <- function(x) "discrete"
52+
53+
#' @export
54+
scale_type.ordered <- function(x) c("ordinal", "discrete")
55+
56+
#' @export
57+
scale_type.factor <- function(x) "discrete"
58+
59+
#' @export
60+
scale_type.POSIXt <- function(x) c("datetime", "continuous")
61+
62+
#' @export
63+
scale_type.Date <- function(x) c("date", "continuous")
64+
65+
#' @export
66+
scale_type.numeric <- function(x) "continuous"

R/scales-.r

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ ScalesList <- ggproto("ScalesList", NULL,
1818
},
1919

2020
add = function(self, scale) {
21+
if (is.null(scale)) {
22+
return()
23+
}
24+
2125
prev_aes <- self$find(scale$aesthetics)
2226
if (any(prev_aes)) {
2327
# Get only the first aesthetic name in the returned vector -- it can
@@ -98,14 +102,7 @@ scales_add_defaults <- function(scales, data, aesthetics, env) {
98102
)
99103

100104
for (aes in names(datacols)) {
101-
type <- scale_type(datacols[[aes]])
102-
scale_name <- paste("scale", aes, type, sep = "_")
103-
104-
# Skip aesthetics with no scales (e.g. group, order, etc)
105-
scale_f <- find_global(scale_name, env, mode = "function")
106-
if (is.null(scale_f)) next
107-
108-
scales$add(scale_f())
105+
scales$add(find_scale(aes, datacols[[aes]], env))
109106
}
110107

111108
}
@@ -126,50 +123,3 @@ scales_add_missing <- function(plot, aesthetics, env) {
126123
}
127124

128125

129-
# Look for object first in parent environment and if not found, then in
130-
# ggplot2 namespace environment. This makes it possible to override default
131-
# scales by setting them in the parent environment.
132-
find_global <- function(name, env, mode = "any") {
133-
if (exists(name, envir = env, mode = mode)) {
134-
return(get(name, envir = env, mode = mode))
135-
}
136-
137-
nsenv <- asNamespace("ggplot2")
138-
if (exists(name, envir = nsenv, mode = mode)) {
139-
return(get(name, envir = nsenv, mode = mode))
140-
}
141-
142-
NULL
143-
}
144-
145-
146-
# Determine default type of a scale
147-
scale_type <- function(x) UseMethod("scale_type")
148-
149-
#' @export
150-
scale_type.default <- function(x) {
151-
message("Don't know how to automatically pick scale for object of type ",
152-
paste(class(x), collapse = "/"), ". Defaulting to continuous")
153-
"continuous"
154-
}
155-
156-
#' @export
157-
scale_type.AsIs <- function(x) "identity"
158-
159-
#' @export
160-
scale_type.logical <- function(x) "discrete"
161-
162-
#' @export
163-
scale_type.character <- function(x) "discrete"
164-
165-
#' @export
166-
scale_type.factor <- function(x) "discrete"
167-
168-
#' @export
169-
scale_type.POSIXt <- function(x) "datetime"
170-
171-
#' @export
172-
scale_type.Date <- function(x) "date"
173-
174-
#' @export
175-
scale_type.numeric <- function(x) "continuous"

R/stat-function.r

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#' @param fun function to use
77
#' @param n number of points to interpolate along
88
#' @param args list of additional arguments to pass to \code{fun}
9+
#' @param xlim Optionally, restrict the range of the function to this range.
910
#' @param na.rm If \code{FALSE} (the default), removes missing values with
1011
#' a warning. If \code{TRUE} silently removes missing values.
1112
#' @inheritParams stat_identity
@@ -51,6 +52,7 @@ stat_function <- function(mapping = NULL, data = NULL,
5152
geom = "path", position = "identity",
5253
...,
5354
fun,
55+
xlim = NULL,
5456
n = 101,
5557
args = list(),
5658
na.rm = FALSE,
@@ -69,6 +71,7 @@ stat_function <- function(mapping = NULL, data = NULL,
6971
n = n,
7072
args = args,
7173
na.rm = na.rm,
74+
xlim = xlim,
7275
...
7376
)
7477
)
@@ -81,8 +84,8 @@ stat_function <- function(mapping = NULL, data = NULL,
8184
StatFunction <- ggproto("StatFunction", Stat,
8285
default_aes = aes(y = ..y..),
8386

84-
compute_group = function(data, scales, fun, n = 101, args = list()) {
85-
range <- scales$x$dimension()
87+
compute_group = function(data, scales, fun, xlim = NULL, n = 101, args = list()) {
88+
range <- xlim %||% scales$x$dimension()
8689
xseq <- seq(range[1], range[2], length.out = n)
8790

8891
if (scales$x$is_discrete()) {

R/stat-summary.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' \code{stat_summary} operates on unique \code{x}; \code{stat_summary_bin}
44
#' operators on binned \code{x}. They are more flexible versions of
5-
#' \code{\link{stat_bin}}: instead of just counting, the can compute any
5+
#' \code{\link{stat_bin}}: instead of just counting, they can compute any
66
#' aggregate.
77
#'
88
#' @section Aesthetics:

R/theme-defaults.r

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,20 @@ theme_void <- function(base_size = 12, base_family = "") {
269269
# Use only inherited elements and make everything blank
270270
line = element_blank(),
271271
rect = element_blank(),
272-
text = element_blank(),
272+
text = element_text(
273+
family = base_family, face = "plain",
274+
colour = "black", size = base_size,
275+
lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0,
276+
margin = margin(), debug = FALSE
277+
),
273278
plot.margin = unit(c(0, 0, 0, 0), "lines"),
279+
axis.text.x = element_blank(),
280+
axis.text.y = element_blank(),
281+
axis.title.x = element_blank(),
282+
axis.title.y = element_blank(),
283+
legend.text = element_text(size = rel(0.8)),
284+
legend.title = element_blank(),
285+
strip.text = element_text(size = rel(0.8)),
274286

275287
complete = TRUE
276288
)

R/zxx.r

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ scale_colour_discrete <- scale_colour_hue
1010
#' @usage NULL
1111
scale_colour_continuous <- scale_colour_gradient
1212

13+
#' @export
14+
#' @rdname scale_gradient
15+
#' @usage NULL
16+
scale_colour_datetime <- function() {
17+
scale_colour_continuous(trans = "time")
18+
}
19+
20+
#' @export
21+
#' @rdname scale_gradient
22+
#' @usage NULL
23+
scale_colour_date <- function() {
24+
scale_colour_continuous(trans = "date")
25+
}
26+
1327
#' @export
1428
#' @rdname scale_hue
1529
#' @usage NULL
@@ -20,6 +34,21 @@ scale_fill_discrete <- scale_fill_hue
2034
#' @usage NULL
2135
scale_fill_continuous <- scale_fill_gradient
2236

37+
#' @export
38+
#' @rdname scale_gradient
39+
#' @usage NULL
40+
scale_fill_datetime <- function() {
41+
scale_fill_continuous(trans = "time")
42+
}
43+
44+
#' @export
45+
#' @rdname scale_gradient
46+
#' @usage NULL
47+
scale_fill_date <- function() {
48+
scale_fill_continuous(trans = "date")
49+
}
50+
51+
2352
# British to American spellings ----------------------------------------------
2453

2554
#' @export

man/scale_gradient.Rd

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

0 commit comments

Comments
 (0)