Skip to content

Restore axis line when breaks = NULL. #5838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* 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
Expand Down
23 changes: 17 additions & 6 deletions R/guide-axis-theta.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,22 @@ GuideAxisTheta <- ggproto(

transform = function(params, coord, panel_params) {

opposite <- setdiff(c("x", "y"), params$aesthetic)
params$key[[opposite]] <- switch(params$position,
theta.sec = -Inf,
top = -Inf,
right = -Inf,
Inf)
if (nrow(params$key) > 0) {
opposite <- setdiff(c("x", "y"), params$aesthetic)
params$key[[opposite]] <- switch(params$position,
theta.sec = -Inf,
top = -Inf,
right = -Inf,
Inf)
}

params <- GuideAxis$transform(params, coord, panel_params)

key <- params$key
n <- nrow(key)
if (n < 1) {
return(params)
}

if (!("theta" %in% names(key))) {
# We likely have a linear coord, so we match the text angles to
Expand Down Expand Up @@ -242,6 +247,12 @@ GuideAxisTheta <- ggproto(
grobTree(major, minor, name = "ticks")
},

draw_early_exit = function(self, params, elements) {
line <- self$build_decor(decor = params$decor, elements = elements,
params = params)
gTree(children = gList(line), offset = unit(0, "cm"))
},

measure_grobs = function(grobs, params, elements) {
# As this guide is expected to be placed in the interior of coord_radial,
# we don't need to measure grob sizes nor arrange the layout.
Expand Down
46 changes: 28 additions & 18 deletions R/guide-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ GuideAxis <- ggproto(

extract_key = function(scale, aesthetic, minor.ticks = FALSE, ...) {
major <- Guide$extract_key(scale, aesthetic, ...)
if (is.null(major) && is.null(scale$scale$get_breaks())) {
major <- data_frame0()
}
if (!minor.ticks) {
return(major)
}
Expand Down Expand Up @@ -147,10 +150,11 @@ GuideAxis <- ggproto(
extract_decor = function(scale, aesthetic, position, key, cap = "none", ...) {

value <- c(-Inf, Inf)
if (cap %in% c("both", "upper")) {
has_key <- !(is.null(key) || nrow(key) < 1)
if (cap %in% c("both", "upper") && has_key) {
value[2] <- max(key[[aesthetic]])
}
if (cap %in% c("both", "lower")) {
if (cap %in% c("both", "lower") && has_key) {
value[1] <- min(key[[aesthetic]])
}

Expand All @@ -166,26 +170,32 @@ GuideAxis <- ggproto(
transform = function(self, params, coord, panel_params) {
key <- params$key
position <- params$position

if (is.null(position) || nrow(key) == 0) {
return(params)
check <- FALSE

if (!(is.null(position) || nrow(key) == 0)) {
check <- TRUE
aesthetics <- names(key)[!grepl("^\\.", names(key))]
if (!all(c("x", "y") %in% aesthetics)) {
other_aesthetic <- setdiff(c("x", "y"), aesthetics)
override_value <- if (position %in% c("bottom", "left")) -Inf else Inf
key[[other_aesthetic]] <- override_value
}
key <- coord$transform(key, panel_params)
params$key <- key
}

aesthetics <- names(key)[!grepl("^\\.", names(key))]
if (!all(c("x", "y") %in% aesthetics)) {
other_aesthetic <- setdiff(c("x", "y"), aesthetics)
override_value <- if (position %in% c("bottom", "left")) -Inf else Inf
key[[other_aesthetic]] <- override_value
}
key <- coord$transform(key, panel_params)
params$key <- key
if (!is.null(params$decor)) {
params$decor <- coord_munch(coord, params$decor, panel_params)

params$decor <- coord_munch(coord, params$decor, panel_params)
if (!coord$is_linear()) {
# For non-linear coords, we hardcode the opposite position
params$decor$x <- switch(position, left = 1, right = 0, params$decor$x)
params$decor$y <- switch(position, top = 0, bottom = 1, params$decor$y)
}
}

if (!coord$is_linear()) {
# For non-linear coords, we hardcode the opposite position
params$decor$x <- switch(position, left = 1, right = 0, params$decor$x)
params$decor$y <- switch(position, top = 0, bottom = 1, params$decor$y)
if (!check) {
return(params)
}

# Ported over from `warn_for_position_guide`
Expand Down