Skip to content

Commit

Permalink
Merge branch 'main' into facet_sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Apr 29, 2024
2 parents 6f2ce12 + 09bcda6 commit a7cde1d
Show file tree
Hide file tree
Showing 55 changed files with 432 additions and 163 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ export(get_alt_text)
export(get_element_tree)
export(get_guide_data)
export(gg_dep)
export(ggpar)
export(ggplot)
export(ggplotGrob)
export(ggplot_add)
Expand Down
16 changes: 15 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

* `facet_wrap()` has new options for the `dir` argument to more precisely
control panel directions (@teunbrand, #5212)
* `geom_curve()` now appropriately removes missing data instead of throwing
errors (@teunbrand, #5831).
* `update_geom_defaults()` and `update_stat_defaults()` have a reset mechanism
when using `new = NULL` and invisible return the previous defaults (#4993).
* Fixed regression in axes where `breaks = NULL` caused the axes to disappear
instead of just rendering the axis line (@teunbrand, #5816).
* `geom_point()` can be dodged vertically by using
`position_dodge(..., orientation = "y")` (@teunbrand, #5809).
* Fixed bug where `na.value` was incorrectly mapped to non-`NA` values
(@teunbrand, #5756).
* Fixed bug in `guide_custom()` that would throw error with `theme_void()`
(@teunbrand, #5856).
* New helper function `ggpar()` to translate ggplot2's interpretation of
graphical parameters to {grid}'s interpretation (@teunbrand, #5866).

# ggplot2 3.5.1

Expand All @@ -23,7 +37,7 @@ documentation updates.
(@teunbrand, #5757).

## Improvements

* When facets coerce the faceting variables to factors, the 'ordered' class
is dropped (@teunbrand, #5666).
* `coord_map()` and `coord_polar()` throw informative warnings when used
Expand Down
8 changes: 4 additions & 4 deletions R/annotation-logticks.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ GeomLogticks <- ggproto("GeomLogticks", Geom,
ticks$x_b <- with(data, segmentsGrob(
x0 = unit(xticks$x, "native"), x1 = unit(xticks$x, "native"),
y0 = unit(xticks$start, "cm"), y1 = unit(xticks$end, "cm"),
gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth * .pt)
gp = ggpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth)
))
}
if (grepl("t", sides) && nrow(xticks) > 0) {
ticks$x_t <- with(data, segmentsGrob(
x0 = unit(xticks$x, "native"), x1 = unit(xticks$x, "native"),
y0 = unit(1, "npc") - unit(xticks$start, "cm"), y1 = unit(1, "npc") - unit(xticks$end, "cm"),
gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth * .pt)
gp = ggpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth)
))
}
}
Expand Down Expand Up @@ -213,14 +213,14 @@ GeomLogticks <- ggproto("GeomLogticks", Geom,
ticks$y_l <- with(data, segmentsGrob(
y0 = unit(yticks$y, "native"), y1 = unit(yticks$y, "native"),
x0 = unit(yticks$start, "cm"), x1 = unit(yticks$end, "cm"),
gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth * .pt)
gp = ggpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth)
))
}
if (grepl("r", sides) && nrow(yticks) > 0) {
ticks$y_r <- with(data, segmentsGrob(
y0 = unit(yticks$y, "native"), y1 = unit(yticks$y, "native"),
x0 = unit(1, "npc") - unit(yticks$start, "cm"), x1 = unit(1, "npc") - unit(yticks$end, "cm"),
gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth * .pt)
gp = ggpar(col = alpha(colour, alpha), lty = linetype, lwd = linewidth)
))
}
}
Expand Down
4 changes: 2 additions & 2 deletions R/annotation-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ GeomAnnotationMap <- ggproto("GeomAnnotationMap", GeomMap,

polygonGrob(coords$x, coords$y, default.units = "native",
id = grob_id,
gp = gpar(
gp = ggpar(
col = data$colour, fill = alpha(data$fill, data$alpha),
lwd = data$linewidth * .pt)
lwd = data$linewidth)
)
},

Expand Down
4 changes: 2 additions & 2 deletions R/coord-radial.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ CoordRadial <- ggproto("CoordRadial", Coord,
y = c(Inf, -Inf, -Inf, Inf)
)
background <- coord_munch(self, background, panel_params, is_closed = TRUE)
bg_gp <- gpar(
lwd = len0_null(bg_element$linewidth * .pt),
bg_gp <- ggpar(
lwd = bg_element$linewidth,
col = bg_element$colour, fill = bg_element$fill,
lty = bg_element$linetype
)
Expand Down
4 changes: 2 additions & 2 deletions R/coord-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
if (inherits(el, "element_blank")) {
grobs <- list(element_render(theme, "panel.background"))
} else {
line_gp <- gpar(
line_gp <- ggpar(
col = el$colour,
lwd = len0_null(el$linewidth * .pt),
lwd = el$linewidth,
lty = el$linetype
)
grobs <- c(
Expand Down
9 changes: 7 additions & 2 deletions R/geom-curve.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ GeomCurve <- ggproto("GeomCurve", GeomSegment,
if (!coord$is_linear()) {
cli::cli_warn("{.fn geom_curve} is not implemented for non-linear coordinates")
}
data <- remove_missing(
data, na.rm = na.rm,
c("x", "y", "xend", "yend", "linetype", "linewidth"),
name = "geom_curve"
)

trans <- coord$transform(data, panel_params)

Expand All @@ -57,10 +62,10 @@ GeomCurve <- ggproto("GeomCurve", GeomSegment,
default.units = "native",
curvature = curvature, angle = angle, ncp = ncp,
square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE,
gp = gpar(
gp = ggpar(
col = alpha(trans$colour, trans$alpha),
fill = alpha(arrow.fill, trans$alpha),
lwd = trans$linewidth * .pt,
lwd = trans$linewidth,
lty = trans$linetype,
lineend = lineend),
arrow = arrow
Expand Down
54 changes: 37 additions & 17 deletions R/geom-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#' @param stat,geom Name of geom/stat to modify (like `"point"` or
#' `"bin"`), or a Geom/Stat object (like `GeomPoint` or
#' `StatBin`).
#' @param new Named list of aesthetics.
#' @param new One of the following:
#' * A named list of aesthetics to serve as new defaults.
#' * `NULL` to reset the defaults.
#' @keywords internal
#' @export
#' @examples
Expand All @@ -16,7 +18,7 @@
#' ggplot(mtcars, aes(mpg, wt)) + geom_point()
#'
#' # reset default
#' update_geom_defaults("point", aes(color = "black"))
#' update_geom_defaults("point", NULL)
#'
#'
#' # updating a stat's default aesthetic settings
Expand All @@ -29,27 +31,45 @@
#' geom_function(fun = dnorm, color = "red")
#'
#' # reset default
#' update_stat_defaults("bin", aes(y = after_stat(count)))
#' update_stat_defaults("bin", NULL)
#'
#' @rdname update_defaults
update_geom_defaults <- function(geom, new) {
g <- check_subclass(geom, "Geom", env = parent.frame())
old <- g$default_aes
new <- rename_aes(new)
new_names_order <- unique(c(names(old), names(new)))
new <- defaults(new, old)[new_names_order]
g$default_aes[names(new)] <- new
invisible()
update_defaults(geom, "Geom", new, env = parent.frame())
}

#' @rdname update_defaults
#' @export
update_stat_defaults <- function(stat, new) {
g <- check_subclass(stat, "Stat", env = parent.frame())
old <- g$default_aes
new <- rename_aes(new)
new_names_order <- unique(c(names(old), names(new)))
new <- defaults(new, old)[new_names_order]
g$default_aes[names(new)] <- new
invisible()
update_defaults(stat, "Stat", new, env = parent.frame())
}

cache_defaults <- new_environment()

update_defaults <- function(name, subclass, new, env = parent.frame()) {
obj <- check_subclass(name, subclass, env = env)
index <- snake_class(obj)

if (is.null(new)) { # Reset from cache

old <- cache_defaults[[index]]
if (!is.null(old)) {
new <- update_defaults(name, subclass, new = old, env = env)
}
invisible(new)

} else { # Update default aesthetics

old <- obj$default_aes
# Only update cache the first time defaults are changed
if (!exists(index, envir = cache_defaults)) {
cache_defaults[[index]] <- old
}
new <- rename_aes(new)
name_order <- unique(c(names(old), names(new)))
new <- defaults(new, old)[name_order]
obj$default_aes[names(new)] <- new
invisible(old)

}
}
4 changes: 2 additions & 2 deletions R/geom-dotplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
dotstackGrob(stackaxis = stackaxis, x = tdata$x, y = tdata$y, dotdia = dotdianpc,
stackposition = tdata$stackpos, stackdir = stackdir, stackratio = stackratio,
default.units = "npc",
gp = gpar(col = alpha(tdata$colour, tdata$alpha),
gp = ggpar(col = alpha(tdata$colour, tdata$alpha),
fill = fill_alpha(tdata$fill, tdata$alpha),
lwd = tdata$stroke, lty = tdata$linetype,
lwd = tdata$stroke / .pt, lty = tdata$linetype,
lineend = lineend))
)
},
Expand Down
4 changes: 2 additions & 2 deletions R/geom-hex.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ GeomHex <- ggproto("GeomHex", Geom,

ggname("geom_hex", polygonGrob(
coords$x, coords$y,
gp = gpar(
gp = ggpar(
col = data$colour,
fill = fill_alpha(data$fill, data$alpha),
lwd = data$linewidth * .pt,
lwd = data$linewidth,
lty = data$linetype,
lineend = lineend,
linejoin = linejoin,
Expand Down
10 changes: 5 additions & 5 deletions R/geom-label.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ GeomLabel <- ggproto("GeomLabel", Geom,
padding = label.padding,
r = label.r,
angle = row$angle,
text.gp = gpar(
text.gp = ggpar(
col = row$colour,
fontsize = row$size * size.unit,
fontfamily = row$family,
fontface = row$fontface,
lineheight = row$lineheight
),
rect.gp = gpar(
rect.gp = ggpar(
col = if (isTRUE(all.equal(label.size, 0))) NA else row$colour,
fill = fill_alpha(row$fill, row$alpha),
lwd = label.size * .pt
lwd = label.size
)
)
})
Expand All @@ -115,7 +115,7 @@ GeomLabel <- ggproto("GeomLabel", Geom,
labelGrob <- function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"),
just = "center", padding = unit(0.25, "lines"), r = unit(0.1, "snpc"),
angle = NULL, default.units = "npc", name = NULL,
text.gp = gpar(), rect.gp = gpar(fill = "white"), vp = NULL) {
text.gp = gpar(), rect.gp = ggpar(fill = "white"), vp = NULL) {

if (length(label) != 1) {
cli::cli_abort("{.arg label} must be of length 1.")
Expand All @@ -130,7 +130,7 @@ labelGrob <- function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"),
vp <- viewport(
angle = angle, x = x, y = y,
width = unit(0, "cm"), height = unit(0, "cm"),
gp = gpar(fontsize = text.gp$fontsize)
gp = ggpar(fontsize = text.gp$fontsize)
)
x <- unit(rep(0.5, length(x)), "npc")
y <- unit(rep(0.5, length(y)), "npc")
Expand Down
4 changes: 2 additions & 2 deletions R/geom-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ GeomMap <- ggproto("GeomMap", GeomPolygon,
data <- data[data_rows, , drop = FALSE]

polygonGrob(coords$x, coords$y, default.units = "native", id = grob_id,
gp = gpar(
gp = ggpar(
col = data$colour,
fill = fill_alpha(data$fill, data$alpha),
lwd = data$linewidth * .pt,
lwd = data$linewidth,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
Expand Down
8 changes: 4 additions & 4 deletions R/geom-path.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ GeomPath <- ggproto("GeomPath", Geom,
segmentsGrob(
munched$x[!end], munched$y[!end], munched$x[!start], munched$y[!start],
default.units = "native", arrow = arrow,
gp = gpar(
gp = ggpar(
col = alpha(munched$colour, munched$alpha)[!end],
fill = alpha(munched$colour, munched$alpha)[!end],
lwd = munched$linewidth[!end] * .pt,
lwd = munched$linewidth[!end],
lty = munched$linetype[!end],
lineend = lineend,
linejoin = linejoin,
Expand All @@ -215,10 +215,10 @@ GeomPath <- ggproto("GeomPath", Geom,
polylineGrob(
munched$x, munched$y, id = id,
default.units = "native", arrow = arrow,
gp = gpar(
gp = ggpar(
col = alpha(munched$colour, munched$alpha)[start],
fill = alpha(munched$colour, munched$alpha)[start],
lwd = munched$linewidth[start] * .pt,
lwd = munched$linewidth[start],
lty = munched$linetype[start],
lineend = lineend,
linejoin = linejoin,
Expand Down
11 changes: 4 additions & 7 deletions R/geom-point.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#' `geom_point(alpha = 0.05)`) or very small (e.g.
#' `geom_point(shape = ".")`).
#'
#' @eval rd_aesthetics("geom", "point")
#' @eval rd_aesthetics("geom", "point", "The `fill` aesthetic only applies to shapes 21-25.")
#' @inheritParams layer
#' @param na.rm If `FALSE`, the default, missing values are removed with
#' a warning. If `TRUE`, missing values are silently removed.
Expand Down Expand Up @@ -145,18 +145,15 @@ GeomPoint <- ggproto("GeomPoint", Geom,
}

coords <- coord$transform(data, panel_params)
stroke_size <- coords$stroke
stroke_size[is.na(stroke_size)] <- 0
ggname("geom_point",
pointsGrob(
coords$x, coords$y,
pch = coords$shape,
gp = gpar(
gp = ggpar(
col = alpha(coords$colour, coords$alpha),
fill = fill_alpha(coords$fill, coords$alpha),
# Stroke is added around the outside of the point
fontsize = coords$size * .pt + stroke_size * .stroke / 2,
lwd = coords$stroke * .stroke / 2
pointsize = coords$size,
stroke = coords$stroke
)
)
)
Expand Down
8 changes: 4 additions & 4 deletions R/geom-polygon.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ GeomPolygon <- ggproto("GeomPolygon", Geom,
polygonGrob(
munched$x, munched$y, default.units = "native",
id = munched$group,
gp = gpar(
gp = ggpar(
col = first_rows$colour,
fill = fill_alpha(first_rows$fill, first_rows$alpha),
lwd = first_rows$linewidth * .pt,
lwd = first_rows$linewidth,
lty = first_rows$linetype,
lineend = lineend,
linejoin = linejoin,
Expand Down Expand Up @@ -161,10 +161,10 @@ GeomPolygon <- ggproto("GeomPolygon", Geom,
munched$x, munched$y, default.units = "native",
id = id, pathId = munched$group,
rule = rule,
gp = gpar(
gp = ggpar(
col = first_rows$colour,
fill = fill_alpha(first_rows$fill, first_rows$alpha),
lwd = first_rows$linewidth * .pt,
lwd = first_rows$linewidth,
lty = first_rows$linetype,
lineend = lineend,
linejoin = linejoin,
Expand Down
4 changes: 2 additions & 2 deletions R/geom-rect.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ GeomRect <- ggproto("GeomRect", Geom,
height = coords$ymax - coords$ymin,
default.units = "native",
just = c("left", "top"),
gp = gpar(
gp = ggpar(
col = coords$colour,
fill = fill_alpha(coords$fill, coords$alpha),
lwd = coords$linewidth * .pt,
lwd = coords$linewidth,
lty = coords$linetype,
linejoin = linejoin,
lineend = lineend
Expand Down

0 comments on commit a7cde1d

Please sign in to comment.