Skip to content

Commit

Permalink
resolve merge conflict
Browse files Browse the repository at this point in the history
Merge branch 'main' into coord_reversal

# Conflicts:
#	R/coord-.R
#	man/coord_trans.Rd
  • Loading branch information
teunbrand committed Sep 2, 2024
2 parents 1403eac + 57ba97f commit 615bf9a
Show file tree
Hide file tree
Showing 42 changed files with 409 additions and 204 deletions.
10 changes: 9 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* `coord_radial()` displays minor gridlines now (@teunbrand).
* (internal) `continuous_scale()` and `binned_scale()` sort the `limits`
argument internally (@teunbrand).
* `guide_bins()`, `guide_colourbar()` and `guide_coloursteps()` gain an `angle`
argument to overrule theme settings, similar to `guide_axis(angle)`
(@teunbrand, #4594).
* `coord_*(expand)` can now take a logical vector to control expansion at any
side of the panel (top, right, bottom, left) (@teunbrand, #6020)
* (Breaking) The defaults for all geoms can be set at one in the theme.
(@teunbrand based on pioneering work by @dpseidel, #2239)
* A new `theme(geom)` argument is used to track these defaults.
Expand All @@ -29,7 +34,7 @@
class through new `Coord$draw_panel()` method.
* `theme(strip.clip)` now defaults to `"on"` and is independent of Coord
clipping (@teunbrand, 5952).
* (internal) rearranged the code of `Facet$draw_paensl()` method (@teunbrand).
* (internal) rearranged the code of `Facet$draw_panels()` method (@teunbrand).
* Axis labels are now justified across facet panels (@teunbrand, #5820)
* Fixed bug in `stat_function()` so x-axis title now produced automatically
when no data added. (@phispu, #5647).
Expand Down Expand Up @@ -175,6 +180,9 @@
* `theme_classic()` now has black ticks and text instead of dark gray. In
addition, `theme_classic()`'s axis line end is `"square"` (@teunbrand, #5978).
* {tibble} is now suggested instead of imported (@teunbrand, #5986)
* The ellipsis argument is now checked in `fortify()`, `get_alt_text()`,
`labs()` and several guides (@teunbrand, #3196).
* `stat_summary_bin()` no longer ignores `width` parameter (@teunbrand, #4647).

# ggplot2 3.5.1

Expand Down
22 changes: 21 additions & 1 deletion R/coord-.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Coord <- ggproto("Coord",
is_free = function() FALSE,

setup_params = function(data) {
list()
list(expand = parse_coord_expand(self$expand %||% TRUE))
},

setup_data = function(data, params = list()) {
Expand Down Expand Up @@ -243,6 +243,26 @@ render_axis <- function(panel_params, axis, scale, position, theme) {
}
}

# Elaborates an 'expand' argument for every side (top, right, bottom or left)
parse_coord_expand <- function(expand) {
check_logical(expand)
if (anyNA(expand)) {
cli::cli_abort("{.arg expand} cannot contain missing values.")
}

if (!is_named(expand)) {
return(rep_len(expand, 4))
}

# Match by top/right/bottom/left
out <- rep(TRUE, 4)
i <- match(names(expand), .trbl)
if (sum(!is.na(i)) > 0) {
out[i] <- unname(expand)[!is.na(i)]
}
out
}

# Utility function to check coord limits
check_coord_limits <- function(
limits, arg = caller_arg(limits), call = caller_env()
Expand Down
8 changes: 6 additions & 2 deletions R/coord-cartesian-.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#' @param expand If `TRUE`, the default, adds a small expansion factor to
#' the limits to ensure that data and axes don't overlap. If `FALSE`,
#' limits are taken exactly from the data or `xlim`/`ylim`.
#' Giving a logical vector will separately control the expansion for the four
#' directions (top, left, bottom and right). The `expand` argument will be
#' recycled to length 4 if necessary. Alternatively, can be a named logical
#' vector to control a single direction, e.g. `expand = c(bottom = FALSE)`.
#' @param default Is this the default coordinate system? If `FALSE` (the default),
#' then replacing this coordinate system with another one creates a message alerting
#' the user that the coordinate system is being replaced. If `TRUE`, that warning
Expand Down Expand Up @@ -108,8 +112,8 @@ CoordCartesian <- ggproto("CoordCartesian", Coord,

setup_panel_params = function(self, scale_x, scale_y, params = list()) {
c(
view_scales_from_scale(scale_x, self$limits$x, self$expand),
view_scales_from_scale(scale_y, self$limits$y, self$expand)
view_scales_from_scale(scale_x, self$limits$x, params$expand[c(4, 2)]),
view_scales_from_scale(scale_y, self$limits$y, params$expand[c(3, 1)])
)
},

Expand Down
1 change: 1 addition & 0 deletions R/coord-flip.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ CoordFlip <- ggproto("CoordFlip", CoordCartesian,
},

setup_panel_params = function(self, scale_x, scale_y, params = list()) {
params$expand <- params$expand[c(2, 1, 4, 3)]
parent <- ggproto_parent(CoordCartesian, self)
panel_params <- parent$setup_panel_params(scale_x, scale_y, params)
flip_axis_labels(panel_params)
Expand Down
37 changes: 18 additions & 19 deletions R/coord-radial.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ coord_radial <- function(theta = "x",
}
reverse <- arg_match0(reverse, c("theta", "thetar", "r", "none"))

check_bool(expand)
check_bool(rotate.angle)
check_number_decimal(start, allow_infinite = FALSE)
check_number_decimal(end, allow_infinite = FALSE, allow_null = TRUE)
Expand Down Expand Up @@ -150,8 +149,8 @@ CoordRadial <- ggproto("CoordRadial", Coord,
setup_panel_params = function(self, scale_x, scale_y, params = list()) {

params <- c(
view_scales_polar(scale_x, self$theta, expand = self$expand),
view_scales_polar(scale_y, self$theta, expand = self$expand),
view_scales_polar(scale_x, self$theta, expand = params$expand[c(4, 2)]),
view_scales_polar(scale_y, self$theta, expand = params$expand[c(3, 1)]),
list(bbox = polar_bbox(self$arc, inner_radius = self$inner_radius),
arc = self$arc, inner_radius = self$inner_radius)
)
Expand Down Expand Up @@ -419,27 +418,27 @@ CoordRadial <- ggproto("CoordRadial", Coord,
},

setup_params = function(self, data) {
if (isFALSE(self$r_axis_inside)) {
place <- in_arc(c(0, 0.5, 1, 1.5) * pi, self$arc)
if (place[1]) {
return(list(r_axis = "left", fake_arc = c(0, 2) * pi))
}
if (place[3]) {
return(list(r_axis = "left", fake_arc = c(1, 3)* pi))
}
if (place[2]) {
return(list(r_axis = "bottom", fake_arc = c(0.5, 2.5) * pi))
}
if (place[4]) {
return(list(r_axis = "bottom", fake_arc = c(1.5, 3.5) * pi))
}
params <- ggproto_parent(Coord, self)$setup_params(data)
if (!isFALSE(self$r_axis_inside)) {
return(params)
}

place <- in_arc(c(0, 0.5, 1, 1.5) * pi, self$arc)
if (!any(place)) {
cli::cli_warn(c(
"No appropriate placement found for {.arg r_axis_inside}.",
i = "Axis will be placed at panel edge."
))
self$r_axis_inside <- TRUE
params$r_axis_inside <- TRUE
return(params)
}
return(NULL)

params$r_axis <- if (any(place[c(1, 3)])) "left" else "bottom"
params$fake_arc <- switch(
which(place[c(1, 3, 2, 4)])[1],
c(0, 2), c(1, 3), c(0.5, 2.5), c(1.5, 3.5)
) * pi
params
}
)

Expand Down
12 changes: 5 additions & 7 deletions R/coord-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
},

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

params <- list(
crs = crs,
default_crs = self$default_crs
)
params$crs <- self$determine_crs(data)
params$default_crs <- self$default_crs
self$params <- params

params
Expand Down Expand Up @@ -174,8 +172,8 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,

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

# get scale limits and coord limits and merge together
# coord limits take precedence over scale limits
Expand Down
4 changes: 2 additions & 2 deletions R/coord-transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ CoordTrans <- ggproto("CoordTrans", Coord,

setup_panel_params = function(self, scale_x, scale_y, params = list()) {
c(
view_scales_from_scale_with_coord_trans(scale_x, self$limits$x, self$trans$x, self$expand),
view_scales_from_scale_with_coord_trans(scale_y, self$limits$y, self$trans$y, self$expand)
view_scales_from_scale_with_coord_trans(scale_x, self$limits$x, self$trans$x, params$expand[c(4, 2)]),
view_scales_from_scale_with_coord_trans(scale_y, self$limits$y, self$trans$y, params$expand[c(3, 1)])
)
},

Expand Down
7 changes: 5 additions & 2 deletions R/fortify.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
#' @seealso [fortify.lm()]
#' @param model model or other R object to convert to data frame
#' @param data original dataset, if needed
#' @param ... other arguments passed to methods
#' @inheritParams rlang::args_dots_used
#' @export
fortify <- function(model, data, ...) UseMethod("fortify")
fortify <- function(model, data, ...) {
warn_dots_used()
UseMethod("fortify")
}

#' @export
fortify.data.frame <- function(model, data, ...) model
Expand Down
1 change: 1 addition & 0 deletions R/geom-hex.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' the very regular alignment of [geom_bin_2d()].
#'
#' @eval rd_aesthetics("geom", "hex")
#' @eval rd_aesthetics("stat", "binhex")
#' @seealso [stat_bin_2d()] for rectangular binning
#' @param geom,stat Override the default connection between `geom_hex()` and
#' `stat_bin_hex()`. For more information about overriding these connections,
Expand Down
78 changes: 28 additions & 50 deletions R/guide-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,8 @@ GuideAxis <- ggproto(
},

override_elements = function(params, elements, theme) {
label <- elements$text
if (!inherits(label, "element_text")) {
return(elements)
}
label_overrides <- axis_label_element_overrides(
params$position, params$angle
)
# label_overrides is an element_text, but label_element may not be;
# to merge the two elements, we just copy angle, hjust, and vjust
# unless their values are NULL
label$angle <- label_overrides$angle %||% label$angle
label$hjust <- label_overrides$hjust %||% label$hjust
label$vjust <- label_overrides$vjust %||% label$vjust

elements$text <- label
elements$text <-
label_angle_heuristic(elements$text, params$position, params$angle)
return(elements)
},

Expand Down Expand Up @@ -584,49 +571,40 @@ axis_label_priority_between <- function(x, y) {
)
}

#' Override axis text angle and alignment
#' Override text angle and alignment
#'
#' @param element An `element_text()`
#' @param axis_position One of bottom, left, top, or right
#' @param angle The text angle, or NULL to override nothing
#'
#' @return An [element_text()] that contains parameters that should be
#' overridden from the user- or theme-supplied element.
#' @noRd
#'
axis_label_element_overrides <- function(axis_position, angle = NULL) {

if (is.null(angle) || is.waive(angle)) {
return(element_text(angle = NULL, hjust = NULL, vjust = NULL))
label_angle_heuristic <- function(element, position, angle) {
if (!inherits(element, "element_text")
|| is.null(position)
|| is.null(angle %|W|% NULL)) {
return(element)
}
arg_match0(position, .trbl)

check_number_decimal(angle)
angle <- angle %% 360
arg_match0(
axis_position,
c("bottom", "left", "top", "right")
)

if (axis_position == "bottom") {

hjust <- if (angle %in% c(0, 180)) 0.5 else if (angle < 180) 1 else 0
vjust <- if (angle %in% c(90, 270)) 0.5 else if (angle > 90 & angle < 270) 0 else 1

} else if (axis_position == "left") {

hjust <- if (angle %in% c(90, 270)) 0.5 else if (angle > 90 & angle < 270) 0 else 1
vjust <- if (angle %in% c(0, 180)) 0.5 else if (angle < 180) 0 else 1

} else if (axis_position == "top") {

hjust <- if (angle %in% c(0, 180)) 0.5 else if (angle < 180) 0 else 1
vjust <- if (angle %in% c(90, 270)) 0.5 else if (angle > 90 & angle < 270) 1 else 0

} else if (axis_position == "right") {

hjust <- if (angle %in% c(90, 270)) 0.5 else if (angle > 90 & angle < 270) 1 else 0
vjust <- if (angle %in% c(0, 180)) 0.5 else if (angle < 180) 1 else 0

}

element_text(angle = angle, hjust = hjust, vjust = vjust)
radian <- deg2rad(angle)
digits <- 3

# Taking the sign of the (co)sine snaps the value to c(-1, 0, 1)
# Doing `x / 2 + 0.5` rescales it to c(0, 0.5, 1), which are good values for justification
# The rounding step ensures we can get (co)sine to exact 0 so it can become 0.5
# which we need for center-justifications
cosine <- sign(round(cos(radian), digits)) / 2 + 0.5
sine <- sign(round(sin(radian), digits)) / 2 + 0.5

# Depending on position, we might need to swap or flip justification values
hjust <- switch(position, left = cosine, right = 1 - cosine, top = 1 - sine, sine)
vjust <- switch(position, left = 1 - sine, right = sine, top = 1 - cosine, cosine)

element$angle <- angle %||% element$angle
element$hjust <- hjust %||% element$hjust
element$vjust <- vjust %||% element$vjust
element
}
7 changes: 7 additions & 0 deletions R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ NULL
#' guide if they are mapped in the same way.
#'
#' @inheritParams guide_legend
#' @param angle Overrules the theme settings to automatically apply appropriate
#' `hjust` and `vjust` for angled legend text. Can be a single number
#' representing the text angle in degrees, or `NULL` to not overrule the
#' settings (default).
#' @param show.limits Logical. Should the limits of the scale be shown with
#' labels and ticks. Default is `NULL` meaning it will take the value from the
#' scale. This argument is ignored if `labels` is given as a vector of
Expand Down Expand Up @@ -65,6 +69,7 @@ guide_bins <- function(
theme = NULL,

# general
angle = NULL,
position = NULL,
direction = NULL,
override.aes = list(),
Expand All @@ -85,6 +90,7 @@ guide_bins <- function(
theme = theme,

# general
angle = angle,
position = position,
direction = direction,
override.aes = rename_aes(override.aes),
Expand Down Expand Up @@ -115,6 +121,7 @@ GuideBins <- ggproto(
default_axis = element_line("black", linewidth = (0.5 / .pt)),
default_ticks = element_line(inherit.blank = TRUE),

angle = NULL,
direction = NULL,
override.aes = list(),
reverse = FALSE,
Expand Down
7 changes: 7 additions & 0 deletions R/guide-colorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ NULL
#' @param alpha A numeric between 0 and 1 setting the colour transparency of
#' the bar. Use `NA` to preserve the alpha encoded in the colour itself
#' (default).
#' @param angle Overrules the theme settings to automatically apply appropriate
#' `hjust` and `vjust` for angled legend text. Can be a single number
#' representing the text angle in degrees, or `NULL` to not overrule the
#' settings (default).
#' @param draw.ulim A logical specifying if the upper limit tick marks should
#' be visible.
#' @param draw.llim A logical specifying if the lower limit tick marks should
Expand Down Expand Up @@ -124,6 +128,7 @@ guide_colourbar <- function(
alpha = NA,
draw.ulim = TRUE,
draw.llim = TRUE,
angle = NULL,
position = NULL,
direction = NULL,
reverse = FALSE,
Expand Down Expand Up @@ -151,6 +156,7 @@ guide_colourbar <- function(
nbin = nbin,
display = display,
alpha = alpha,
angle = angle,
draw_lim = c(isTRUE(draw.llim), isTRUE(draw.ulim)),
position = position,
direction = direction,
Expand Down Expand Up @@ -193,6 +199,7 @@ GuideColourbar <- ggproto(
direction = NULL,
reverse = FALSE,
order = 0,
angle = NULL,

# parameter
name = "colourbar",
Expand Down
Loading

0 comments on commit 615bf9a

Please sign in to comment.