Skip to content

Commit f87e548

Browse files
authored
Merge branch 'main' into theme_geom_defaults
2 parents 6a08f2d + 7fb4c38 commit f87e548

18 files changed

+85
-38
lines changed

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* The `element_geom()` function can be used to populate that argument.
77
* The `from_theme()` function allows access to the theme default fields from
88
inside the `aes()` function.
9+
* Missing values from discrete palettes are no longer translated
10+
(@teunbrand, #5929).
11+
* Fixed bug in `facet_grid(margins = TRUE)` when using expresssions
12+
(@teunbrand, #1864).
913
* `geom_step()` now supports the `orientation` argument (@teunbrand, #5936).
1014
* `position_dodge()` and `position_jitterdodge()` now have a `reverse` argument
1115
(@teunbrand, #3610)
@@ -133,6 +137,8 @@
133137
* New function `get_strip_labels()` to retrieve facet labels (@teunbrand, #4979)
134138
* Fixed bug in `position_dodge2()`'s identification of range overlaps
135139
(@teunbrand, #5938, #4327).
140+
* Fixed bug where empty discrete scales weren't recognised as such
141+
(@teunbrand, #5945).
136142

137143
# ggplot2 3.5.1
138144

R/facet-grid-.R

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,21 @@ FacetGrid <- ggproto("FacetGrid", Facet,
297297
return(data)
298298
}
299299

300-
# Compute faceting values and add margins
301-
margin_vars <- list(intersect(names(rows), names(data)),
302-
intersect(names(cols), names(data)))
303-
data <- reshape_add_margins(data, margin_vars, params$margins)
304-
300+
# Compute faceting values
305301
facet_vals <- eval_facets(c(rows, cols), data, params$.possible_columns)
302+
if (nrow(facet_vals) == nrow(data)) {
303+
# Margins are computed on evaluated faceting values (#1864).
304+
facet_vals <- reshape_add_margins(
305+
# We add an index column to track data recycling
306+
vec_cbind(facet_vals, .index = seq_len(nrow(facet_vals))),
307+
list(intersect(names(rows), names(facet_vals)),
308+
intersect(names(cols), names(facet_vals))),
309+
params$margins
310+
)
311+
# Apply recycling on original data to fit margins
312+
data <- vec_slice(data, facet_vals$.index)
313+
facet_vals$.index <- NULL
314+
}
306315

307316
# If any faceting variables are missing, add them in by
308317
# duplicating the data

R/labels.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ setup_plot_labels <- function(plot, layers, data) {
2424
# Find labels from every layer
2525
for (i in seq_along(layers)) {
2626
layer <- layers[[i]]
27+
exclude <- names(layer$aes_params)
2728
mapping <- layer$computed_mapping
2829
mapping <- strip_stage(mapping)
2930
mapping <- strip_dots(mapping, strip_pronoun = TRUE)
31+
mapping <- mapping[setdiff(names(mapping), exclude)]
3032

3133
# Acquire default labels
3234
mapping_default <- make_labels(mapping)

R/scale-.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -973,23 +973,23 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
973973
self$n.breaks.cache <- n
974974
}
975975

976-
if (!is_null(names(pal))) {
976+
na_value <- if (self$na.translate) self$na.value else NA
977+
pal_names <- names(pal)
978+
979+
if (!is_null(pal_names)) {
977980
# if pal is named, limit the pal by the names first,
978981
# then limit the values by the pal
979-
idx_nomatch <- is.na(match(names(pal), limits))
980-
pal[idx_nomatch] <- NA
981-
pal_match <- pal[match(as.character(x), names(pal))]
982-
pal_match <- unname(pal_match)
983-
} else {
984-
# if pal is not named, limit the values directly
985-
pal_match <- pal[match(as.character(x), limits)]
982+
pal[is.na(match(pal_names, limits))] <- na_value
983+
pal <- unname(pal)
984+
limits <- pal_names
986985
}
986+
pal <- c(pal, na_value)
987+
pal_match <- pal[match(as.character(x), limits, nomatch = length(pal))]
987988

988-
if (self$na.translate) {
989-
ifelse(is.na(x) | is.na(pal_match), self$na.value, pal_match)
990-
} else {
991-
pal_match
989+
if (!is.na(na_value)) {
990+
pal_match[is.na(x)] <- na_value
992991
}
992+
pal_match
993993
},
994994

995995
rescale = function(self, x, limits = self$get_limits(), range = c(1, length(limits))) {

R/scale-discrete-.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,
130130
},
131131

132132
is_empty = function(self) {
133-
is.null(self$range$range) && is.null(self$limits) && is.null(self$range_c$range)
133+
is.null(self$range$range) &&
134+
(is.null(self$limits) || is.function(self$limits)) &&
135+
is.null(self$range_c$range)
134136
},
135137

136138
reset = function(self) {

R/scale-gradient.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#' @param low,high Colours for low and high ends of the gradient.
1717
#' @param guide Type of legend. Use `"colourbar"` for continuous
1818
#' colour bar, or `"legend"` for discrete colour legend.
19-
#' @inheritDotParams continuous_scale -na.value -guide -aesthetics -expand -position
19+
#' @inheritDotParams continuous_scale -na.value -guide -aesthetics -expand -position -palette
2020
#' @seealso [scales::pal_seq_gradient()] for details on underlying
2121
#' palette, [scale_colour_steps()] for binned variants of these scales.
2222
#'

R/scale-grey.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#'
66
#' @inheritParams scales::pal_grey
77
#' @inheritParams scale_colour_hue
8-
#' @inheritDotParams discrete_scale -expand -position -scale_name
8+
#' @inheritDotParams discrete_scale -expand -position -scale_name -palette
99
#' @family colour scales
1010
#' @seealso
1111
#' The documentation on [colour aesthetics][aes_colour_fill_alpha].

R/scale-hue.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#' It does not generate colour-blind safe palettes.
55
#'
66
#' @param na.value Colour to use for missing values
7-
#' @inheritDotParams discrete_scale -aesthetics -expand -position -scale_name
7+
#' @inheritDotParams discrete_scale -aesthetics -expand -position -scale_name -palette
88
#' @param aesthetics Character string or vector of character strings listing the
99
#' name(s) of the aesthetic(s) that this scale works with. This can be useful, for
1010
#' example, to apply colour settings to the `colour` and `fill` aesthetics at the

R/scale-linetype.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#' no inherent order, this use is not advised.
77
#'
88
#' @inheritParams scale_x_discrete
9-
#' @inheritDotParams discrete_scale -expand -position -na.value -scale_name
9+
#' @inheritDotParams discrete_scale -expand -position -na.value -scale_name -palette
1010
#' @param na.value The linetype to use for `NA` values.
1111
#' @rdname scale_linetype
1212
#' @seealso

R/scale-shape.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' @param solid Should the shapes be solid, `TRUE`, or hollow,
1111
#' `FALSE`?
1212
#' @inheritParams scale_x_discrete
13-
#' @inheritDotParams discrete_scale -expand -position -scale_name
13+
#' @inheritDotParams discrete_scale -expand -position -scale_name -palette
1414
#' @rdname scale_shape
1515
#' @seealso
1616
#' The documentation for [differentiation related aesthetics][aes_linetype_size_shape].

0 commit comments

Comments
 (0)