Skip to content

Commit 6885cfb

Browse files
authored
Merge branch 'main' into attr_stability
2 parents 6b113c9 + 4af509e commit 6885cfb

Some content is hidden

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

43 files changed

+810
-276
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ export(theme_minimal)
721721
export(theme_replace)
722722
export(theme_set)
723723
export(theme_test)
724+
export(theme_transparent)
724725
export(theme_update)
725726
export(theme_void)
726727
export(transform_position)

NEWS.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
* (internal) layer data can be attenuated with parameter attributes
44
(@teunbrand, #3175).
5+
* New argument `labs(dictionary)` to label based on variable name rather than
6+
based on aesthetic (@teunbrand, #5178)
7+
* Fixed bug in out-of-bounds binned breaks (@teunbrand, #6054)
8+
* Binned guides now accept expressions as labels (@teunbrand, #6005)
9+
* (internal) `Scale$get_labels()` format expressions as lists.
510
* In non-orthogonal coordinate systems (`coord_sf()`, `coord_polar()` and
611
`coord_radial()`), using 'AsIs' variables escape transformation when
712
both `x` and `y` is an 'AsIs' variable (@teunbrand, #6205).
@@ -214,6 +219,12 @@
214219
* `stat_summary_bin()` no longer ignores `width` parameter (@teunbrand, #4647).
215220
* Added `keep.zeroes` argument to `stat_bin()` (@teunbrand, #3449)
216221
* `coord_sf()` no longer errors when dealing with empty graticules (@teunbrand, #6052)
222+
* Added `theme_transparent()` with transparent backgrounds (@topepo).
223+
* New theme elements `palette.{aes}.discrete` and `palette.{aes}.continuous`.
224+
Theme palettes replace palettes in scales where `palette = NULL`, which is
225+
the new default in many scales (@teunbrand, #4696).
226+
* `guide_axis()` no longer reserves space for blank ticks
227+
(@teunbrand, #4722, #6069).
217228

218229
# ggplot2 3.5.1
219230

R/geom-text.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
#' # Add aesthetic mappings
9696
#' p + geom_text(aes(colour = factor(cyl)))
9797
#' p + geom_text(aes(colour = factor(cyl))) +
98-
#' scale_colour_discrete(l = 40)
98+
#' scale_colour_hue(l = 40)
9999
#' p + geom_label(aes(fill = factor(cyl)), colour = "white", fontface = "bold")
100100
#'
101101
#' # Scale size of text, and change legend key glyph from a to point

R/guide-.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,8 @@ Guide <- ggproto(
225225

226226
mapped <- scale$map(breaks)
227227
labels <- scale$get_labels(breaks)
228-
# {vctrs} doesn't play nice with expressions, convert to list.
229-
# see also https://github.com/r-lib/vctrs/issues/559
230-
if (is.expression(labels)) {
231-
labels <- as.list(labels)
232-
}
233228

234-
key <- data_frame(mapped, .name_repair = ~ aesthetic)
229+
key <- data_frame(!!aesthetic := mapped)
235230
key$.value <- breaks
236231
key$.label <- labels
237232

R/guide-axis-theta.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ GuideAxisTheta <- ggproto(
110110
# labels of these positions
111111
ends_apart <- (key$theta[n] - key$theta[1]) %% (2 * pi)
112112
if (n > 0 && ends_apart < 0.05 && !is.null(key$.label)) {
113-
if (is.expression(key$.label)) {
113+
if (is.expression(key$.label[[1]])) {
114114
combined <- substitute(
115115
paste(a, "/", b),
116116
list(a = key$.label[[1]], b = key$.label[[n]])

R/guide-axis.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ GuideAxis <- ggproto(
256256
override_elements = function(params, elements, theme) {
257257
elements$text <-
258258
label_angle_heuristic(elements$text, params$position, params$angle)
259+
if (inherits(elements$ticks, "element_blank")) {
260+
elements$major_length <- unit(0, "cm")
261+
}
262+
if (inherits(elements$minor, "element_blank") || isFALSE(params$minor.ticks)) {
263+
elements$minor_length <- unit(0, "cm")
264+
}
259265
return(elements)
260266
},
261267

R/guide-bins.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ GuideBins <- ggproto(
161161
key$.show <- NA
162162

163163
labels <- scale$get_labels(breaks)
164-
if (is.character(scale$labels) || is.numeric(scale$labels)) {
164+
labels <- labels[!is.na(breaks)]
165+
breaks <- breaks[!is.na(breaks)]
166+
167+
if (is.character(scale$labels) || is.numeric(scale$labels) || is.expression(scale$labels)) {
165168
limit_lab <- c(NA, NA)
166169
} else {
167170
limit_lab <- scale$get_labels(limits)
@@ -265,7 +268,7 @@ GuideBins <- ggproto(
265268

266269
list(labels = flip_element_grob(
267270
elements$text,
268-
label = key$.label,
271+
label = validate_labels(key$.label),
269272
x = unit(key$.value, "npc"),
270273
margin_x = FALSE,
271274
margin_y = TRUE,
@@ -335,19 +338,22 @@ GuideBins <- ggproto(
335338

336339
parse_binned_breaks <- function(scale, breaks = scale$get_breaks()) {
337340

338-
breaks <- breaks[!is.na(breaks)]
341+
if (is.waiver(scale$labels) || is.function(scale$labels)) {
342+
breaks <- breaks[!is.na(breaks)]
343+
}
339344
if (length(breaks) == 0) {
340345
return(NULL)
341346
}
342347

343348
if (is.numeric(breaks)) {
344-
breaks <- sort(breaks)
345349
limits <- scale$get_limits()
346350
if (!is.numeric(scale$breaks)) {
347-
breaks <- breaks[!breaks %in% limits]
351+
breaks[breaks %in% limits] <- NA
348352
}
349-
breaks <- oob_discard(breaks, limits)
353+
breaks <- oob_censor(breaks, limits)
350354
all_breaks <- unique0(c(limits[1], breaks, limits[2]))
355+
# Sorting drops NAs on purpose here
356+
all_breaks <- sort(all_breaks, na.last = NA)
351357
bin_at <- all_breaks[-1] - diff(all_breaks) / 2
352358
} else {
353359
bin_at <- breaks

R/guide-colorsteps.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ GuideColoursteps <- ggproto(
112112

113113
key <- data_frame0(!!aesthetic := scale$map(breaks))
114114
if (even.steps) {
115-
key$.value <- seq_along(breaks)
115+
key$.value <- NA_integer_
116+
key$.value[!is.na(breaks)] <- seq_along(breaks[!is.na(breaks)])
116117
} else {
117118
key$.value <- breaks
118119
}
119120
key$.label <- scale$get_labels(breaks)
121+
key <- vec_slice(key, !is.na(breaks))
120122

121123
if (breaks[1] %in% limits) {
122124
key$.value <- key$.value - 1L

R/labels.R

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ setup_plot_labels <- function(plot, layers, data) {
8484
))
8585
}
8686

87+
dict <- plot_labels$dictionary
88+
if (length(dict) > 0) {
89+
labels <- lapply(labels, function(x) {
90+
dict <- dict[names(dict) %in% x]
91+
x[match(names(dict), x)] <- dict
92+
x
93+
})
94+
}
95+
8796
defaults(plot_labels, labels)
8897
}
8998

@@ -114,6 +123,10 @@ setup_plot_labels <- function(plot, layers, data) {
114123
#' bottom-right of the plot by default.
115124
#' @param tag The text for the tag label which will be displayed at the
116125
#' top-left of the plot by default.
126+
#' @param dictionary A named character vector to serve as dictionary.
127+
#' Automatically derived labels, such as those based on variables will
128+
#' be matched with `names(dictionary)` and replaced by the matching
129+
#' entry in `dictionary`.
117130
#' @param alt,alt_insight Text used for the generation of alt-text for the plot.
118131
#' See [get_alt_text] for examples. `alt` can also be a function that
119132
#' takes the plot as input and returns text as output. `alt` also accepts
@@ -128,6 +141,14 @@ setup_plot_labels <- function(plot, layers, data) {
128141
#' p + labs(colour = "Cylinders")
129142
#' p + labs(x = "New x label")
130143
#'
144+
#' # Set labels by variable name instead of aesthetic
145+
#' p + labs(dict = c(
146+
#' disp = "Displacment", # Not in use
147+
#' cyl = "Number of cylinders",
148+
#' mpg = "Miles per gallon",
149+
#' wt = "Weight (1000 lbs)"
150+
#' ))
151+
#'
131152
#' # The plot title appears at the top-left, with the subtitle
132153
#' # display in smaller text underneath it
133154
#' p + labs(title = "New plot title")
@@ -146,11 +167,12 @@ setup_plot_labels <- function(plot, layers, data) {
146167
#' labs(title = "title") +
147168
#' labs(title = NULL)
148169
labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(),
149-
tag = waiver(), alt = waiver(), alt_insight = waiver()) {
170+
tag = waiver(), dictionary = waiver(), alt = waiver(),
171+
alt_insight = waiver()) {
150172
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
151173
args <- dots_list(..., title = title, subtitle = subtitle, caption = caption,
152174
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight,
153-
.ignore_empty = "all")
175+
dictionary = dictionary, .ignore_empty = "all")
154176

155177
is_waive <- vapply(args, is.waiver, logical(1))
156178
args <- args[!is_waive]

R/plot-build.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ ggplot_build.ggplot <- function(plot) {
106106
# Train and map non-position scales and guides
107107
npscales <- scales$non_position_scales()
108108
if (npscales$n() > 0) {
109+
npscales$set_palettes(plot$theme)
109110
lapply(data, npscales$train_df)
110111
plot$guides <- plot$guides$build(npscales, plot$layers, plot$labels, data, plot$theme)
111112
data <- lapply(data, npscales$map_df)

0 commit comments

Comments
 (0)