Skip to content

Commit 615bf9a

Browse files
committed
resolve merge conflict
Merge branch 'main' into coord_reversal # Conflicts: # R/coord-.R # man/coord_trans.Rd
2 parents 1403eac + 57ba97f commit 615bf9a

Some content is hidden

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

42 files changed

+409
-204
lines changed

NEWS.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
* `coord_radial()` displays minor gridlines now (@teunbrand).
88
* (internal) `continuous_scale()` and `binned_scale()` sort the `limits`
99
argument internally (@teunbrand).
10+
* `guide_bins()`, `guide_colourbar()` and `guide_coloursteps()` gain an `angle`
11+
argument to overrule theme settings, similar to `guide_axis(angle)`
12+
(@teunbrand, #4594).
13+
* `coord_*(expand)` can now take a logical vector to control expansion at any
14+
side of the panel (top, right, bottom, left) (@teunbrand, #6020)
1015
* (Breaking) The defaults for all geoms can be set at one in the theme.
1116
(@teunbrand based on pioneering work by @dpseidel, #2239)
1217
* A new `theme(geom)` argument is used to track these defaults.
@@ -29,7 +34,7 @@
2934
class through new `Coord$draw_panel()` method.
3035
* `theme(strip.clip)` now defaults to `"on"` and is independent of Coord
3136
clipping (@teunbrand, 5952).
32-
* (internal) rearranged the code of `Facet$draw_paensl()` method (@teunbrand).
37+
* (internal) rearranged the code of `Facet$draw_panels()` method (@teunbrand).
3338
* Axis labels are now justified across facet panels (@teunbrand, #5820)
3439
* Fixed bug in `stat_function()` so x-axis title now produced automatically
3540
when no data added. (@phispu, #5647).
@@ -175,6 +180,9 @@
175180
* `theme_classic()` now has black ticks and text instead of dark gray. In
176181
addition, `theme_classic()`'s axis line end is `"square"` (@teunbrand, #5978).
177182
* {tibble} is now suggested instead of imported (@teunbrand, #5986)
183+
* The ellipsis argument is now checked in `fortify()`, `get_alt_text()`,
184+
`labs()` and several guides (@teunbrand, #3196).
185+
* `stat_summary_bin()` no longer ignores `width` parameter (@teunbrand, #4647).
178186

179187
# ggplot2 3.5.1
180188

R/coord-.R

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Coord <- ggproto("Coord",
188188
is_free = function() FALSE,
189189

190190
setup_params = function(data) {
191-
list()
191+
list(expand = parse_coord_expand(self$expand %||% TRUE))
192192
},
193193

194194
setup_data = function(data, params = list()) {
@@ -243,6 +243,26 @@ render_axis <- function(panel_params, axis, scale, position, theme) {
243243
}
244244
}
245245

246+
# Elaborates an 'expand' argument for every side (top, right, bottom or left)
247+
parse_coord_expand <- function(expand) {
248+
check_logical(expand)
249+
if (anyNA(expand)) {
250+
cli::cli_abort("{.arg expand} cannot contain missing values.")
251+
}
252+
253+
if (!is_named(expand)) {
254+
return(rep_len(expand, 4))
255+
}
256+
257+
# Match by top/right/bottom/left
258+
out <- rep(TRUE, 4)
259+
i <- match(names(expand), .trbl)
260+
if (sum(!is.na(i)) > 0) {
261+
out[i] <- unname(expand)[!is.na(i)]
262+
}
263+
out
264+
}
265+
246266
# Utility function to check coord limits
247267
check_coord_limits <- function(
248268
limits, arg = caller_arg(limits), call = caller_env()

R/coord-cartesian-.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#' @param expand If `TRUE`, the default, adds a small expansion factor to
1010
#' the limits to ensure that data and axes don't overlap. If `FALSE`,
1111
#' limits are taken exactly from the data or `xlim`/`ylim`.
12+
#' Giving a logical vector will separately control the expansion for the four
13+
#' directions (top, left, bottom and right). The `expand` argument will be
14+
#' recycled to length 4 if necessary. Alternatively, can be a named logical
15+
#' vector to control a single direction, e.g. `expand = c(bottom = FALSE)`.
1216
#' @param default Is this the default coordinate system? If `FALSE` (the default),
1317
#' then replacing this coordinate system with another one creates a message alerting
1418
#' the user that the coordinate system is being replaced. If `TRUE`, that warning
@@ -108,8 +112,8 @@ CoordCartesian <- ggproto("CoordCartesian", Coord,
108112

109113
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
110114
c(
111-
view_scales_from_scale(scale_x, self$limits$x, self$expand),
112-
view_scales_from_scale(scale_y, self$limits$y, self$expand)
115+
view_scales_from_scale(scale_x, self$limits$x, params$expand[c(4, 2)]),
116+
view_scales_from_scale(scale_y, self$limits$y, params$expand[c(3, 1)])
113117
)
114118
},
115119

R/coord-flip.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ CoordFlip <- ggproto("CoordFlip", CoordCartesian,
8989
},
9090

9191
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
92+
params$expand <- params$expand[c(2, 1, 4, 3)]
9293
parent <- ggproto_parent(CoordCartesian, self)
9394
panel_params <- parent$setup_panel_params(scale_x, scale_y, params)
9495
flip_axis_labels(panel_params)

R/coord-radial.R

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ coord_radial <- function(theta = "x",
7878
}
7979
reverse <- arg_match0(reverse, c("theta", "thetar", "r", "none"))
8080

81-
check_bool(expand)
8281
check_bool(rotate.angle)
8382
check_number_decimal(start, allow_infinite = FALSE)
8483
check_number_decimal(end, allow_infinite = FALSE, allow_null = TRUE)
@@ -150,8 +149,8 @@ CoordRadial <- ggproto("CoordRadial", Coord,
150149
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
151150

152151
params <- c(
153-
view_scales_polar(scale_x, self$theta, expand = self$expand),
154-
view_scales_polar(scale_y, self$theta, expand = self$expand),
152+
view_scales_polar(scale_x, self$theta, expand = params$expand[c(4, 2)]),
153+
view_scales_polar(scale_y, self$theta, expand = params$expand[c(3, 1)]),
155154
list(bbox = polar_bbox(self$arc, inner_radius = self$inner_radius),
156155
arc = self$arc, inner_radius = self$inner_radius)
157156
)
@@ -419,27 +418,27 @@ CoordRadial <- ggproto("CoordRadial", Coord,
419418
},
420419

421420
setup_params = function(self, data) {
422-
if (isFALSE(self$r_axis_inside)) {
423-
place <- in_arc(c(0, 0.5, 1, 1.5) * pi, self$arc)
424-
if (place[1]) {
425-
return(list(r_axis = "left", fake_arc = c(0, 2) * pi))
426-
}
427-
if (place[3]) {
428-
return(list(r_axis = "left", fake_arc = c(1, 3)* pi))
429-
}
430-
if (place[2]) {
431-
return(list(r_axis = "bottom", fake_arc = c(0.5, 2.5) * pi))
432-
}
433-
if (place[4]) {
434-
return(list(r_axis = "bottom", fake_arc = c(1.5, 3.5) * pi))
435-
}
421+
params <- ggproto_parent(Coord, self)$setup_params(data)
422+
if (!isFALSE(self$r_axis_inside)) {
423+
return(params)
424+
}
425+
426+
place <- in_arc(c(0, 0.5, 1, 1.5) * pi, self$arc)
427+
if (!any(place)) {
436428
cli::cli_warn(c(
437429
"No appropriate placement found for {.arg r_axis_inside}.",
438430
i = "Axis will be placed at panel edge."
439431
))
440-
self$r_axis_inside <- TRUE
432+
params$r_axis_inside <- TRUE
433+
return(params)
441434
}
442-
return(NULL)
435+
436+
params$r_axis <- if (any(place[c(1, 3)])) "left" else "bottom"
437+
params$fake_arc <- switch(
438+
which(place[c(1, 3, 2, 4)])[1],
439+
c(0, 2), c(1, 3), c(0.5, 2.5), c(1.5, 3.5)
440+
) * pi
441+
params
443442
}
444443
)
445444

R/coord-sf.R

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
1818
},
1919

2020
setup_params = function(self, data) {
21-
crs <- self$determine_crs(data)
21+
params <- ggproto_parent(Coord, self)$setup_params(data)
2222

23-
params <- list(
24-
crs = crs,
25-
default_crs = self$default_crs
26-
)
23+
params$crs <- self$determine_crs(data)
24+
params$default_crs <- self$default_crs
2725
self$params <- params
2826

2927
params
@@ -174,8 +172,8 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
174172

175173
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
176174
# expansion factors for scale limits
177-
expansion_x <- default_expansion(scale_x, expand = self$expand)
178-
expansion_y <- default_expansion(scale_y, expand = self$expand)
175+
expansion_x <- default_expansion(scale_x, expand = params$expand[c(4, 2)])
176+
expansion_y <- default_expansion(scale_y, expand = params$expand[c(3, 1)])
179177

180178
# get scale limits and coord limits and merge together
181179
# coord limits take precedence over scale limits

R/coord-transform.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ CoordTrans <- ggproto("CoordTrans", Coord,
158158

159159
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
160160
c(
161-
view_scales_from_scale_with_coord_trans(scale_x, self$limits$x, self$trans$x, self$expand),
162-
view_scales_from_scale_with_coord_trans(scale_y, self$limits$y, self$trans$y, self$expand)
161+
view_scales_from_scale_with_coord_trans(scale_x, self$limits$x, self$trans$x, params$expand[c(4, 2)]),
162+
view_scales_from_scale_with_coord_trans(scale_y, self$limits$y, self$trans$y, params$expand[c(3, 1)])
163163
)
164164
},
165165

R/fortify.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
#' @seealso [fortify.lm()]
99
#' @param model model or other R object to convert to data frame
1010
#' @param data original dataset, if needed
11-
#' @param ... other arguments passed to methods
11+
#' @inheritParams rlang::args_dots_used
1212
#' @export
13-
fortify <- function(model, data, ...) UseMethod("fortify")
13+
fortify <- function(model, data, ...) {
14+
warn_dots_used()
15+
UseMethod("fortify")
16+
}
1417

1518
#' @export
1619
fortify.data.frame <- function(model, data, ...) model

R/geom-hex.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#' the very regular alignment of [geom_bin_2d()].
77
#'
88
#' @eval rd_aesthetics("geom", "hex")
9+
#' @eval rd_aesthetics("stat", "binhex")
910
#' @seealso [stat_bin_2d()] for rectangular binning
1011
#' @param geom,stat Override the default connection between `geom_hex()` and
1112
#' `stat_bin_hex()`. For more information about overriding these connections,

R/guide-axis.R

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -254,21 +254,8 @@ GuideAxis <- ggproto(
254254
},
255255

256256
override_elements = function(params, elements, theme) {
257-
label <- elements$text
258-
if (!inherits(label, "element_text")) {
259-
return(elements)
260-
}
261-
label_overrides <- axis_label_element_overrides(
262-
params$position, params$angle
263-
)
264-
# label_overrides is an element_text, but label_element may not be;
265-
# to merge the two elements, we just copy angle, hjust, and vjust
266-
# unless their values are NULL
267-
label$angle <- label_overrides$angle %||% label$angle
268-
label$hjust <- label_overrides$hjust %||% label$hjust
269-
label$vjust <- label_overrides$vjust %||% label$vjust
270-
271-
elements$text <- label
257+
elements$text <-
258+
label_angle_heuristic(elements$text, params$position, params$angle)
272259
return(elements)
273260
},
274261

@@ -584,49 +571,40 @@ axis_label_priority_between <- function(x, y) {
584571
)
585572
}
586573

587-
#' Override axis text angle and alignment
574+
#' Override text angle and alignment
588575
#'
576+
#' @param element An `element_text()`
589577
#' @param axis_position One of bottom, left, top, or right
590578
#' @param angle The text angle, or NULL to override nothing
591579
#'
592580
#' @return An [element_text()] that contains parameters that should be
593581
#' overridden from the user- or theme-supplied element.
594582
#' @noRd
595-
#'
596-
axis_label_element_overrides <- function(axis_position, angle = NULL) {
597-
598-
if (is.null(angle) || is.waive(angle)) {
599-
return(element_text(angle = NULL, hjust = NULL, vjust = NULL))
583+
label_angle_heuristic <- function(element, position, angle) {
584+
if (!inherits(element, "element_text")
585+
|| is.null(position)
586+
|| is.null(angle %|W|% NULL)) {
587+
return(element)
600588
}
589+
arg_match0(position, .trbl)
601590

602591
check_number_decimal(angle)
603-
angle <- angle %% 360
604-
arg_match0(
605-
axis_position,
606-
c("bottom", "left", "top", "right")
607-
)
608-
609-
if (axis_position == "bottom") {
610-
611-
hjust <- if (angle %in% c(0, 180)) 0.5 else if (angle < 180) 1 else 0
612-
vjust <- if (angle %in% c(90, 270)) 0.5 else if (angle > 90 & angle < 270) 0 else 1
613-
614-
} else if (axis_position == "left") {
615-
616-
hjust <- if (angle %in% c(90, 270)) 0.5 else if (angle > 90 & angle < 270) 0 else 1
617-
vjust <- if (angle %in% c(0, 180)) 0.5 else if (angle < 180) 0 else 1
618-
619-
} else if (axis_position == "top") {
620-
621-
hjust <- if (angle %in% c(0, 180)) 0.5 else if (angle < 180) 0 else 1
622-
vjust <- if (angle %in% c(90, 270)) 0.5 else if (angle > 90 & angle < 270) 1 else 0
623-
624-
} else if (axis_position == "right") {
625-
626-
hjust <- if (angle %in% c(90, 270)) 0.5 else if (angle > 90 & angle < 270) 1 else 0
627-
vjust <- if (angle %in% c(0, 180)) 0.5 else if (angle < 180) 1 else 0
628-
629-
}
630-
631-
element_text(angle = angle, hjust = hjust, vjust = vjust)
592+
radian <- deg2rad(angle)
593+
digits <- 3
594+
595+
# Taking the sign of the (co)sine snaps the value to c(-1, 0, 1)
596+
# Doing `x / 2 + 0.5` rescales it to c(0, 0.5, 1), which are good values for justification
597+
# The rounding step ensures we can get (co)sine to exact 0 so it can become 0.5
598+
# which we need for center-justifications
599+
cosine <- sign(round(cos(radian), digits)) / 2 + 0.5
600+
sine <- sign(round(sin(radian), digits)) / 2 + 0.5
601+
602+
# Depending on position, we might need to swap or flip justification values
603+
hjust <- switch(position, left = cosine, right = 1 - cosine, top = 1 - sine, sine)
604+
vjust <- switch(position, left = 1 - sine, right = sine, top = 1 - cosine, cosine)
605+
606+
element$angle <- angle %||% element$angle
607+
element$hjust <- hjust %||% element$hjust
608+
element$vjust <- vjust %||% element$vjust
609+
element
632610
}

0 commit comments

Comments
 (0)