Skip to content

Commit 073958d

Browse files
authored
Restore axis line when breaks = NULL. (#5838)
* preserve empty keys * Don't skip decor transformation when key is empty * add news bullet * deal with 0-length breaks in `guide_axis_theta()` * capping deals with empty keys
1 parent 5dd2633 commit 073958d

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* Fixed regression in axes where `breaks = NULL` caused the axes to disappear
4+
instead of just rendering the axis line (@teunbrand, #5816).
35
* `geom_point()` can be dodged vertically by using
46
`position_dodge(..., orientation = "y")` (@teunbrand, #5809).
57
* Fixed bug where `na.value` was incorrectly mapped to non-`NA` values

R/guide-axis-theta.R

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,22 @@ GuideAxisTheta <- ggproto(
7474

7575
transform = function(params, coord, panel_params) {
7676

77-
opposite <- setdiff(c("x", "y"), params$aesthetic)
78-
params$key[[opposite]] <- switch(params$position,
79-
theta.sec = -Inf,
80-
top = -Inf,
81-
right = -Inf,
82-
Inf)
77+
if (nrow(params$key) > 0) {
78+
opposite <- setdiff(c("x", "y"), params$aesthetic)
79+
params$key[[opposite]] <- switch(params$position,
80+
theta.sec = -Inf,
81+
top = -Inf,
82+
right = -Inf,
83+
Inf)
84+
}
8385

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

8688
key <- params$key
8789
n <- nrow(key)
90+
if (n < 1) {
91+
return(params)
92+
}
8893

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

250+
draw_early_exit = function(self, params, elements) {
251+
line <- self$build_decor(decor = params$decor, elements = elements,
252+
params = params)
253+
gTree(children = gList(line), offset = unit(0, "cm"))
254+
},
255+
245256
measure_grobs = function(grobs, params, elements) {
246257
# As this guide is expected to be placed in the interior of coord_radial,
247258
# we don't need to measure grob sizes nor arrange the layout.

R/guide-axis.R

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ GuideAxis <- ggproto(
115115

116116
extract_key = function(scale, aesthetic, minor.ticks = FALSE, ...) {
117117
major <- Guide$extract_key(scale, aesthetic, ...)
118+
if (is.null(major) && is.null(scale$scale$get_breaks())) {
119+
major <- data_frame0()
120+
}
118121
if (!minor.ticks) {
119122
return(major)
120123
}
@@ -147,10 +150,11 @@ GuideAxis <- ggproto(
147150
extract_decor = function(scale, aesthetic, position, key, cap = "none", ...) {
148151

149152
value <- c(-Inf, Inf)
150-
if (cap %in% c("both", "upper")) {
153+
has_key <- !(is.null(key) || nrow(key) < 1)
154+
if (cap %in% c("both", "upper") && has_key) {
151155
value[2] <- max(key[[aesthetic]])
152156
}
153-
if (cap %in% c("both", "lower")) {
157+
if (cap %in% c("both", "lower") && has_key) {
154158
value[1] <- min(key[[aesthetic]])
155159
}
156160

@@ -166,26 +170,32 @@ GuideAxis <- ggproto(
166170
transform = function(self, params, coord, panel_params) {
167171
key <- params$key
168172
position <- params$position
169-
170-
if (is.null(position) || nrow(key) == 0) {
171-
return(params)
173+
check <- FALSE
174+
175+
if (!(is.null(position) || nrow(key) == 0)) {
176+
check <- TRUE
177+
aesthetics <- names(key)[!grepl("^\\.", names(key))]
178+
if (!all(c("x", "y") %in% aesthetics)) {
179+
other_aesthetic <- setdiff(c("x", "y"), aesthetics)
180+
override_value <- if (position %in% c("bottom", "left")) -Inf else Inf
181+
key[[other_aesthetic]] <- override_value
182+
}
183+
key <- coord$transform(key, panel_params)
184+
params$key <- key
172185
}
173186

174-
aesthetics <- names(key)[!grepl("^\\.", names(key))]
175-
if (!all(c("x", "y") %in% aesthetics)) {
176-
other_aesthetic <- setdiff(c("x", "y"), aesthetics)
177-
override_value <- if (position %in% c("bottom", "left")) -Inf else Inf
178-
key[[other_aesthetic]] <- override_value
179-
}
180-
key <- coord$transform(key, panel_params)
181-
params$key <- key
187+
if (!is.null(params$decor)) {
188+
params$decor <- coord_munch(coord, params$decor, panel_params)
182189

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

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

191201
# Ported over from `warn_for_position_guide`

0 commit comments

Comments
 (0)