Skip to content

Commit 21463ab

Browse files
authored
Merge pull request #2 from tidyverse/master
Bringing my repository up-to-date
2 parents 52ea3ac + dcb510e commit 21463ab

27 files changed

+854
-56
lines changed

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Imports:
2323
grDevices,
2424
grid,
2525
gtable (>= 0.1.1),
26-
lazyeval,
2726
MASS,
2827
mgcv,
2928
reshape2,

NAMESPACE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ export(scale_alpha_manual)
426426
export(scale_alpha_ordinal)
427427
export(scale_color_brewer)
428428
export(scale_color_continuous)
429+
export(scale_color_date)
430+
export(scale_color_datetime)
429431
export(scale_color_discrete)
430432
export(scale_color_distiller)
431433
export(scale_color_gradient)
@@ -435,6 +437,7 @@ export(scale_color_grey)
435437
export(scale_color_hue)
436438
export(scale_color_identity)
437439
export(scale_color_manual)
440+
export(scale_color_ordinal)
438441
export(scale_color_viridis_c)
439442
export(scale_color_viridis_d)
440443
export(scale_colour_brewer)
@@ -583,7 +586,6 @@ import(grid)
583586
import(gtable)
584587
import(rlang)
585588
import(scales)
586-
importFrom(lazyeval,f_eval)
587589
importFrom(stats,setNames)
588590
importFrom(tibble,tibble)
589591
importFrom(utils,.DollarNames)

NEWS.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# ggplot2 (development version)
22

3+
* `Geom` now gains a `setup_params()` method in line with the other ggproto
4+
classes (@thomasp85, #3509)
5+
6+
* `element_text()` now issues a warning when vectorized arguments are provided, as in
7+
`colour = c("red", "green", "blue")`. Such use is discouraged and not officially supported
8+
(@clauswilke, #3492).
9+
310
* stacking text when calculating the labels and the y axis with
411
`stat_summary()` now works (@ikosmidis, #2709)
512

@@ -17,10 +24,15 @@
1724

1825
* Changed `theme_grey()` setting for legend key so that it creates no
1926
border (`NA`) rather than drawing a white one. (@annennenne, #3180)
27+
28+
* Themes have gained two new parameters, `plot.title.position` and
29+
`plot.caption.position`, that can be used to customize how plot
30+
title/subtitle and plot caption are positioned relative to the overall plot
31+
(@clauswilke, #3252).
2032

2133
* Added function `ggplot_add.by()` for lists created with `by()` (#2734, @Maschette)
2234

23-
* `ggdep()` was deprecated (@perezp44, #3382).
35+
* `gg_dep()` was deprecated (@perezp44, #3382).
2436

2537
* Added weight aesthetic option to `stat_density()` and made scaling of
2638
weights the default (@annennenne, #2902)
@@ -55,6 +67,10 @@
5567

5668
* `stat_density2d()` can now take an `adjust` parameter to scale the default bandwidth. (#2860, @haleyjeppson)
5769

70+
* `geom_sf()` now removes rows that contain missing `shape`/`size`/`colour` (#3483, @yutannihilation)
71+
72+
* Fix a bug when `show.legend` is a named logical vector (#3461, @yutannihilation).
73+
5874
# ggplot2 3.2.1
5975

6076
This is a patch release fixing a few regressions introduced in 3.2.0 as well as

R/axis-secondary.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ derive <- function() {
120120
is.derived <- function(x) {
121121
inherits(x, "derived")
122122
}
123-
#' @importFrom lazyeval f_eval
124-
#'
125123
#' @rdname ggplot2-ggproto
126124
#' @format NULL
127125
#' @usage NULL

R/facet-.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ is_facets <- function(x) {
419419
# but that seems like a reasonable tradeoff.
420420
eval_facets <- function(facets, data, env = globalenv()) {
421421
vars <- compact(lapply(facets, eval_facet, data, env = env))
422-
tibble::as_tibble(vars)
422+
new_data_frame(tibble::as_tibble(vars))
423423
}
424424
eval_facet <- function(facet, data, env = emptyenv()) {
425425
if (quo_is_symbol(facet)) {

R/geom-.r

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ Geom <- ggproto("Geom",
104104
stop("Not implemented")
105105
},
106106

107+
setup_params = function(data, params) params,
108+
107109
setup_data = function(data, params) data,
108110

109111
# Combine data with defaults and set aesthetics from parameters

R/geom-sf.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ GeomSf <- ggproto("GeomSf", Geom,
9797
stroke = 0.5
9898
),
9999

100+
non_missing_aes = c("size", "shape", "colour"),
101+
100102
draw_panel = function(data, panel_params, coord, legend = NULL,
101103
lineend = "butt", linejoin = "round", linemitre = 10) {
102104
if (!inherits(coord, "CoordSf")) {

R/guide-colorbar.r

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,28 @@ guide_geom.colorbar <- function(guide, layers, default_mapping) {
244244
guide_layers <- lapply(layers, function(layer) {
245245
matched <- matched_aes(layer, guide, default_mapping)
246246

247-
if (length(matched) > 0 && (isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend))) {
247+
if (length(matched) == 0) {
248+
# This layer does not use this guide
249+
return(NULL)
250+
}
251+
252+
# check if this layer should be included, different behaviour depending on
253+
# if show.legend is a logical or a named logical vector
254+
if (is_named(layer$show.legend)) {
255+
layer$show.legend <- rename_aes(layer$show.legend)
256+
show_legend <- layer$show.legend[matched]
257+
# we cannot use `isTRUE(is.na(show_legend))` here because
258+
# 1. show_legend can be multiple NAs
259+
# 2. isTRUE() was not tolerant for a named TRUE
260+
show_legend <- show_legend[!is.na(show_legend)]
261+
include <- length(show_legend) == 0 || any(show_legend)
262+
} else {
263+
include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend)
264+
}
265+
266+
if (include) {
248267
layer
249268
} else {
250-
# This layer does not use this guide
251269
NULL
252270
}
253271
})

R/guide-legend.r

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,14 @@ guide_geom.legend <- function(guide, layers, default_mapping) {
250250

251251
# check if this layer should be included, different behaviour depending on
252252
# if show.legend is a logical or a named logical vector
253-
if (!is.null(names(layer$show.legend))) {
253+
if (is_named(layer$show.legend)) {
254254
layer$show.legend <- rename_aes(layer$show.legend)
255-
include <- is.na(layer$show.legend[matched]) ||
256-
layer$show.legend[matched]
255+
show_legend <- layer$show.legend[matched]
256+
# we cannot use `isTRUE(is.na(show_legend))` here because
257+
# 1. show_legend can be multiple NAs
258+
# 2. isTRUE() was not tolerant for a named TRUE
259+
show_legend <- show_legend[!is.na(show_legend)]
260+
include <- length(show_legend) == 0 || any(show_legend)
257261
} else {
258262
include <- isTRUE(is.na(layer$show.legend)) || isTRUE(layer$show.legend)
259263
}

R/layer.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ Layer <- ggproto("Layer", NULL,
340340
c(names(data), names(self$aes_params)),
341341
snake_class(self$geom)
342342
)
343-
344-
self$geom$setup_data(data, c(self$geom_params, self$aes_params))
343+
self$geom_params <- self$geom$setup_params(data, c(self$geom_params, self$aes_params))
344+
self$geom$setup_data(data, self$geom_params)
345345
},
346346

347347
compute_position = function(self, data, layout) {

R/margins.R

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ title_spec <- function(label, x, y, hjust, vjust, angle, gp = gpar(),
7171
# Use trigonometry to calculate grobheight and width for rotated grobs. This is only
7272
# exactly correct when vjust = 1. We need to take the absolute value so we don't make
7373
# the grob smaller when it's flipped over.
74-
text_height <- unit(1, "grobheight", text_grob) + abs(cos(angle / 180 * pi)) * descent
75-
text_width <- unit(1, "grobwidth", text_grob) + abs(sin(angle / 180 * pi)) * descent
74+
text_height <- unit(1, "grobheight", text_grob) + abs(cos(angle[1] / 180 * pi)) * descent
75+
text_width <- unit(1, "grobwidth", text_grob) + abs(sin(angle[1] / 180 * pi)) * descent
7676

7777
if (isTRUE(debug)) {
7878
children <- gList(
@@ -333,29 +333,36 @@ rotate_just <- function(angle, hjust, vjust) {
333333
list(hjust = hnew, vjust = vnew)
334334
}
335335
descent_cache <- new.env(parent = emptyenv())
336+
# Important: This function is not vectorized. Do not use to look up multiple
337+
# font descents at once.
336338
font_descent <- function(family = "", face = "plain", size = 12, cex = 1) {
337339
cur_dev <- names(grDevices::dev.cur())
340+
if (cur_dev == "null device") {
341+
cache <- FALSE # don't cache if no device open
342+
} else {
343+
cache <- TRUE
344+
}
338345
key <- paste0(cur_dev, ':', family, ':', face, ":", size, ":", cex)
339-
descents <- lapply(key, function(k) {
340-
descent <- descent_cache[[k]]
341-
342-
if (is.null(descent)) {
343-
descent <- convertHeight(grobDescent(textGrob(
344-
label = "gjpqyQ",
345-
gp = gpar(
346-
fontsize = size,
347-
cex = cex,
348-
fontfamily = family,
349-
fontface = face
350-
)
351-
)), 'inches')
352-
descent_cache[[k]] <- descent
346+
# we only look up the first result; this function is not vectorized
347+
key <- key[1]
348+
349+
descent <- descent_cache[[key]]
350+
351+
if (is.null(descent)) {
352+
descent <- convertHeight(grobDescent(textGrob(
353+
label = "gjpqyQ",
354+
gp = gpar(
355+
fontsize = size,
356+
cex = cex,
357+
fontfamily = family,
358+
fontface = face
359+
)
360+
)), 'inches')
361+
362+
if (cache) {
363+
descent_cache[[key]] <- descent
353364
}
354-
descent
355-
})
356-
if (length(descents) == 1) {
357-
descents[[1]]
358-
} else {
359-
do.call(unit.c, descents)
360365
}
366+
367+
descent
361368
}

R/plot-build.r

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,20 +261,46 @@ ggplot_gtable.ggplot_built <- function(data) {
261261
caption <- element_render(theme, "plot.caption", plot$labels$caption, margin_y = TRUE)
262262
caption_height <- grobHeight(caption)
263263

264-
pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), ,
265-
drop = FALSE]
264+
# positioning of title and subtitle is governed by plot.title.position
265+
# positioning of caption is governed by plot.caption.position
266+
# "panel" means align to the panel(s)
267+
# "plot" means align to the entire plot (except margins and tag)
268+
title_pos <- theme$plot.title.position %||% "panel"
269+
if (!(title_pos %in% c("panel", "plot"))) {
270+
stop('plot.title.position should be either "panel" or "plot".', call. = FALSE)
271+
}
272+
caption_pos <- theme$plot.caption.position %||% "panel"
273+
if (!(caption_pos %in% c("panel", "plot"))) {
274+
stop('plot.caption.position should be either "panel" or "plot".', call. = FALSE)
275+
}
276+
277+
pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), , drop = FALSE]
278+
if (title_pos == "panel") {
279+
title_l = min(pans$l)
280+
title_r = max(pans$r)
281+
} else {
282+
title_l = 1
283+
title_r = ncol(plot_table)
284+
}
285+
if (caption_pos == "panel") {
286+
caption_l = min(pans$l)
287+
caption_r = max(pans$r)
288+
} else {
289+
caption_l = 1
290+
caption_r = ncol(plot_table)
291+
}
266292

267293
plot_table <- gtable_add_rows(plot_table, subtitle_height, pos = 0)
268294
plot_table <- gtable_add_grob(plot_table, subtitle, name = "subtitle",
269-
t = 1, b = 1, l = min(pans$l), r = max(pans$r), clip = "off")
295+
t = 1, b = 1, l = title_l, r = title_r, clip = "off")
270296

271297
plot_table <- gtable_add_rows(plot_table, title_height, pos = 0)
272298
plot_table <- gtable_add_grob(plot_table, title, name = "title",
273-
t = 1, b = 1, l = min(pans$l), r = max(pans$r), clip = "off")
299+
t = 1, b = 1, l = title_l, r = title_r, clip = "off")
274300

275301
plot_table <- gtable_add_rows(plot_table, caption_height, pos = -1)
276302
plot_table <- gtable_add_grob(plot_table, caption, name = "caption",
277-
t = -1, b = -1, l = min(pans$l), r = max(pans$r), clip = "off")
303+
t = -1, b = -1, l = caption_l, r = caption_r, clip = "off")
278304

279305
plot_table <- gtable_add_rows(plot_table, unit(0, 'pt'), pos = 0)
280306
plot_table <- gtable_add_cols(plot_table, unit(0, 'pt'), pos = 0)

R/scale-discrete-.r

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,18 @@ ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,
8383
},
8484

8585
get_limits = function(self) {
86-
if (self$is_empty()) {
87-
c(0, 1)
88-
} else if (!is.null(self$limits) & !is.function(self$limits)){
89-
self$limits
90-
} else if (is.null(self$limits)) {
91-
self$range$range
92-
} else if (is.function(self$limits)) {
93-
self$limits(self$range$range)
94-
} else {
95-
integer(0)
96-
}
86+
# if scale contains no information, return the default limit
87+
if (self$is_empty()) {
88+
return(c(0, 1))
89+
}
90+
91+
# if self$limits is not NULL and is a function, apply it to range
92+
if (is.function(self$limits)){
93+
return(self$limits(self$range$range))
94+
}
95+
96+
# self$range$range can be NULL because non-discrete values use self$range_c
97+
self$limits %||% self$range$range %||% integer()
9798
},
9899

99100
is_empty = function(self) {

R/theme-defaults.r

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ theme_grey <- function(base_size = 11, base_family = "",
214214
hjust = 0, vjust = 1,
215215
margin = margin(b = half_line)
216216
),
217+
plot.title.position = "panel",
217218
plot.subtitle = element_text( # font size "regular"
218219
hjust = 0, vjust = 1,
219220
margin = margin(b = half_line)
@@ -223,6 +224,7 @@ theme_grey <- function(base_size = 11, base_family = "",
223224
hjust = 1, vjust = 1,
224225
margin = margin(t = half_line)
225226
),
227+
plot.caption.position = "panel",
226228
plot.tag = element_text(
227229
size = rel(1.2),
228230
hjust = 0.5, vjust = 0.5
@@ -487,6 +489,7 @@ theme_void <- function(base_size = 11, base_family = "",
487489
hjust = 0, vjust = 1,
488490
margin = margin(t = half_line)
489491
),
492+
plot.title.position = "panel",
490493
plot.subtitle = element_text(
491494
hjust = 0, vjust = 1,
492495
margin = margin(t = half_line)
@@ -496,6 +499,7 @@ theme_void <- function(base_size = 11, base_family = "",
496499
hjust = 1, vjust = 1,
497500
margin = margin(t = half_line)
498501
),
502+
plot.caption.position = "panel",
499503
plot.tag = element_text(
500504
size = rel(1.2),
501505
hjust = 0.5, vjust = 0.5
@@ -615,6 +619,7 @@ theme_test <- function(base_size = 11, base_family = "",
615619
hjust = 0, vjust = 1,
616620
margin = margin(b = half_line)
617621
),
622+
plot.title.position = "panel",
618623
plot.subtitle = element_text(
619624
hjust = 0, vjust = 1,
620625
margin = margin(b = half_line)
@@ -624,6 +629,7 @@ theme_test <- function(base_size = 11, base_family = "",
624629
hjust = 1, vjust = 1,
625630
margin = margin(t = half_line)
626631
),
632+
plot.caption.position = "panel",
627633
plot.tag = element_text(
628634
size = rel(1.2),
629635
hjust = 0.5, vjust = 0.5

R/theme-elements.r

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ element_text <- function(family = NULL, face = NULL, colour = NULL,
112112
color = NULL, margin = NULL, debug = NULL, inherit.blank = FALSE) {
113113

114114
if (!is.null(color)) colour <- color
115+
116+
n <- max(
117+
length(family), length(face), length(colour), length(size),
118+
length(hjust), length(vjust), length(angle), length(lineheight)
119+
)
120+
if (n > 1) {
121+
warning(
122+
"Vectorized input to `element_text()` is not officially supported.\n",
123+
"Results may be unexpected or may change in future versions of ggplot2.",
124+
call. = FALSE
125+
)
126+
}
127+
128+
115129
structure(
116130
list(family = family, face = face, colour = colour, size = size,
117131
hjust = hjust, vjust = vjust, angle = angle, lineheight = lineheight,
@@ -356,8 +370,10 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
356370

357371
plot.background = el_def("element_rect", "rect"),
358372
plot.title = el_def("element_text", "title"),
373+
plot.title.position = el_def("character"),
359374
plot.subtitle = el_def("element_text", "title"),
360375
plot.caption = el_def("element_text", "title"),
376+
plot.caption.position = el_def("character"),
361377
plot.tag = el_def("element_text", "title"),
362378
plot.tag.position = el_def("character"), # Need to also accept numbers
363379
plot.margin = el_def("margin"),

0 commit comments

Comments
 (0)